“无法对 System.Single 和 System.String 执行 'Like' 操作。”
'Cannot perform 'Like' operation on System.Single and System.String.'
我正在使用 access 数据库作为数据源构建一个 win form 应用程序,在我的 winform 的搜索按钮中我有这段代码
private void searchAccessDatabase()
{
if (string.IsNullOrEmpty(KeywordTextBox.Text.Trim()))
{
Refreshdata();
return;
}
string strkeyword = KeywordTextBox.Text.Trim().ToString();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
sb.AppendFormat("OR (Customer_Name LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Complaint_Number LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
sb.AppendFormat("OR (Material_Number LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Nature_Of_Problem LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Spool_Type LIKE '*" + "{0}" + "*')", strkeyword);
string strFilter = sb.ToString();
material_Return_DataBindingSource.Filter = strFilter;
if (material_Return_DataBindingSource.Count != 0)
{
dataGridView1.DataSource = material_Return_DataBindingSource;
}
else
{
MessageBox.Show("No Records Found", "Search Result", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
Refreshdata();
return;
}
}
但在 运行 期间出现以下错误:
'无法对 System.Single 和 System.String 执行 'Like' 操作。'
我知道错误与单元格的格式类型和我正在使用的搜索类型有关,但我无法更正它们,因为我仍在学习此问题:任何人请纠正我的错误。
这是示例数据 table :这将帮助您查看我输入 table 的示例数据,我想用它来搜索
- 使用和不使用转换语句进行测试
- 您没有任何 * 来表示字符串相似性可以在哪里结束,例如:
Name LIKE '*test*'
将获取名称字段中带有测试的每条记录,而不管它之前或之后是什么,其中 Name LIKE 'test*
只会为您提供 Name 字段以 test 开头的那些。
您所说的事实 'like' 没有任何通配符或其他指示符的内容可能会导致问题。
试着让它们相等,看看是否出错。
此外,对于多个字符串,您的字符串格式中不需要 +,因为 sb.format 没有它们也能正常工作。
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
可以写成
sb.AppendFormat("(Convert(ID,'System.String') LIKE '{0}')", strkeyword);
我也不是说以下行在开头没有 and OR 这可能会导致错误。
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
也许可以使用附加错误时产生的最终字符串更新您的问题。
我正在使用 access 数据库作为数据源构建一个 win form 应用程序,在我的 winform 的搜索按钮中我有这段代码
private void searchAccessDatabase()
{
if (string.IsNullOrEmpty(KeywordTextBox.Text.Trim()))
{
Refreshdata();
return;
}
string strkeyword = KeywordTextBox.Text.Trim().ToString();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
sb.AppendFormat("OR (Customer_Name LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Complaint_Number LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
sb.AppendFormat("OR (Material_Number LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Nature_Of_Problem LIKE '*" + "{0}" + "*')", strkeyword);
sb.AppendFormat("OR (Spool_Type LIKE '*" + "{0}" + "*')", strkeyword);
string strFilter = sb.ToString();
material_Return_DataBindingSource.Filter = strFilter;
if (material_Return_DataBindingSource.Count != 0)
{
dataGridView1.DataSource = material_Return_DataBindingSource;
}
else
{
MessageBox.Show("No Records Found", "Search Result", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
Refreshdata();
return;
}
}
但在 运行 期间出现以下错误: '无法对 System.Single 和 System.String 执行 'Like' 操作。'
我知道错误与单元格的格式类型和我正在使用的搜索类型有关,但我无法更正它们,因为我仍在学习此问题:任何人请纠正我的错误。
这是示例数据 table :这将帮助您查看我输入 table 的示例数据,我想用它来搜索
- 使用和不使用转换语句进行测试
- 您没有任何 * 来表示字符串相似性可以在哪里结束,例如:
Name LIKE '*test*'
将获取名称字段中带有测试的每条记录,而不管它之前或之后是什么,其中Name LIKE 'test*
只会为您提供 Name 字段以 test 开头的那些。
您所说的事实 'like' 没有任何通配符或其他指示符的内容可能会导致问题。
试着让它们相等,看看是否出错。
此外,对于多个字符串,您的字符串格式中不需要 +,因为 sb.format 没有它们也能正常工作。
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
可以写成
sb.AppendFormat("(Convert(ID,'System.String') LIKE '{0}')", strkeyword);
我也不是说以下行在开头没有 and OR 这可能会导致错误。
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
也许可以使用附加错误时产生的最终字符串更新您的问题。