【问题标题】:'Cannot perform 'Like' operation on System.Single and System.String.''无法对 System.Single 和 System.String 执行 'Like' 操作。
【发布时间】:2020-08-12 05:21:07
【问题描述】:

我正在使用 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(ID,'System.String') LIKE '" + "{0}" + "')" 而不是 "(Convert(ID,'System.String') LIKE '{0}')"
  • 执行.Filter = string.Format("Convert(ID,'System.String') LIKE '{0}' OR Customer_Name LIKE '*{0}*' OR Complaint_Number LIKE '*{0}*' OR Convert([Size],'System.String' LIKE '{0}') OR Material_Number LIKE '*{0}*' OR Nature_Of_Problem LIKE '*{0}*' OR Spool_Type LIKE '*{0}*'"); 会更具可读性,尤其是如果您只创建一个多行字符串并每行放置一个 OR。我注意到唯一看起来像单曲的东西(数量列)不在您的过滤器中。您是在向我们展示完整的代码吗?
  • 感谢您的回复,实际上我不希望过滤数字列,但是我希望过滤大小列,这是单个因此我已添加到代码中。
  • @CaiusJard 我猜他不知道,他确实说他在学习,所以我猜他正在把 'Format()' 当作原生字符串,我确实把它放进去了我在下面回复了一堆其他的东西,包括缺少的 OR 条件。 Ravi,您是否尝试过一次过滤其中一行,然后看看哪一行填满了?
  • 实际上我不想过滤数字列 - 那么为什么过滤器规范包含对它们的引用?

标签: c# winforms ms-access sql-like


【解决方案1】:
  1. 使用和不使用 convert 语句进行测试
  2. 你没有任何 * 来表示字符串相似性可以在哪里结束 例如:Name LIKE '*test*' 将在名称字段中获取每条记录,无论它之前或之后是什么,Name LIKE 'test* 只会让你名称字段以 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);

也许在出错时使用附加结果的最终字符串更新您的问题。

【讨论】:

  • 嗨,slipoch 非常感谢您的回复,正如您所说的缺失或者是问题所在,并且代替 * i 替换为 %,现在它做得很好。
  • 太棒了!很高兴听到它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 2023-04-06
  • 2020-04-15
相关资源
最近更新 更多