Sqlcommand中的IN和Like子句都使用 [英] Both IN and Like clause use in Sqlcommand

查看:82
本文介绍了Sqlcommand中的IN和Like子句都使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何在此处传递与电视或TTV或Coalall等相关名称的变量来代替TV和Coal

how wl i pass the variable here with related names like Tv or TTV or Coalall in place of TV and Coal

string commaDelimited = textBox1.Text;
string[] year = commaDelimited.Split(new char[] { });
          
/////////////////////////////////////////
//string[] r = { "TV", "Radio", "Coal" };
string filter = string.Empty;
foreach (string name in year)
{
    if (filter.Length > 0)
    {
        filter += ",";
    }
    filter += string.Format("''{0}''", name);
}
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ERPDB;Integrated Security=True");
SqlDataAdapter adp;
DataSet ds = new DataSet();
adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName IN (" + filter + ")", con);
adp.Fill(ds, "Prod_Tbl");
dataGridView1.DataSource = ds.Tables[0].DefaultView;

推荐答案

要使用类似的东西,您必须使用类似的东西:

To use like you have to use something like:

Select * from Prod_Tbl where ProductName like '%TV%' OR ProductName LIKE '%RADIO%';


如果您正在寻找完全匹配的内容,那么您编写的代码就可以使用:

If you are looking for exact match then the code which you have written will work:

adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName IN (" + filter + ")", con);



但是,如果要搜索的值不是精确值,则必须使用like运算符.




But if the values which you are search are not exact values then you have to use the like operator.

something like

foreach (string name in year)
{    
if (filter.Length > 0)    
{       
 filter += "AND ";    
}    
filter += string.Format("ProductName  LIKE ''{0}''", name);}



另一种选择:
http://www.eggheadcafe.com/software/aspnet/29398160/combine-in --like.aspx [ ^ ]



Another option:
http://www.eggheadcafe.com/software/aspnet/29398160/combine-in--like.aspx[^]


这篇关于Sqlcommand中的IN和Like子句都使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆