c# - C Sharp 中的 SqlDataAdapter 错误

标签 c# c vb.net

您好,我在 c Sharp 中的数据适配器出现错误。怎么修?

 SqlCommand cmd = new SqlCommand("select * from View_1 where Words_Sh LIKE ' + @txbSearch + '%'", con);
    cmd.Parameters.AddWithValue("@txbSearch", this.txbSearch.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd, con)

;

最佳答案

不要在参数占位符前添加单引号和加号

SqlCommand cmd = new SqlCommand("select * from View_1 " + 
                                "where Words_Sh LIKE @txbSearch + '%'", con);

此外,我更喜欢将通配符直接连接到参数值内。
不过,不确定它是否有任何区别,只是偏好问题和查询字符串中更少的困惑。

 SqlCommand cmd = new SqlCommand("select * from View_1 " + 
                                 "where Words_Sh LIKE @txbSearch", con);
 cmd.Parameters.AddWithValue("@txbSearch", this.txbSearch.Text + "%");

关于c# - C Sharp 中的 SqlDataAdapter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747368/

相关文章:

mysql - 如何保存和保留日期毫秒(MySQL)

c# - yield IEnumerable 的迭代器问题

c# - 如何在 Roslyn 上获取扩展方法?

c# - 为什么这个对象没有被垃圾回收

无法释放从函数返回的 typedef 结构

mysql - 多行数据读取器

c# - 统计程序启动的次数

c++ - 文件 IO,处理 CRLF

c++ - errno 在不同系统上的位置

wpf - 更改标签字符串中单个字母的颜色?