c# - '无法对 System.Single 和 System.String 执行 'Like' 操作。
问题描述
我正在使用 access 数据库作为数据源构建一个 win 表单应用程序,并且在我的 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' 操作。”
我知道该错误与单元格的格式类型和我正在使用的搜索类型有关,但我无法更正它们,因为我仍在学习这一点:任何人都请纠正我的错误。
解决方案
- 使用和不使用 convert 语句进行测试
- 您没有任何 * 来表示字符串相似性可以结束的位置,例如:
Name LIKE '*test*'
将在 Name 字段中获取每条带有 test 的记录,无论它之前或之后是什么,在哪里Name LIKE 'test*
只会让您得到 Name 字段以 test 开头的那些。
您说它“像”没有任何通配符或其他指示符的事实可能会导致问题。
试着让它们相等,看看它是否出错。
此外,您不需要字符串格式中的 + 来处理多个字符串,因为 sb.format 没有它们就可以工作。
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
可以写成
sb.AppendFormat("(Convert(ID,'System.String') LIKE '{0}')", strkeyword);
我也不认为以下行开头没有和 OR,这可能会导致错误。
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
也许在出错时使用附加结果的最终字符串更新您的问题。
推荐阅读
- github - 尝试通过 GitHubActions 发布到 GitHub 包时出现不正确的路径和无 API 错误
- oracle - 在 oracle RAC 中监控 imdp 期间的系统资源消耗
- python - 基于优先级过滤熊猫数据帧的高效/Pythonic方法
- react-native - react native - 自定义博览会通知时间表
- python - Mongodb 似乎加载了两次数据。不知道为什么,也不确定我是否正确加载了 mongodb
- python - 访问 JSON 文件 python if 语句
- javascript - 检测数据何时插入/更新到 sql
- javascript - 如何使用 rxjs 映射通过另一个不同类型的对象列表过滤出一个可观察的对象列表
- progressive-web-apps - 带有 Onesignal、Nuxtjs 和 Prismic CMS 的动态推送通知
- javascript - 如何使用javascript使```var num = 12;```等于3?