SQL通配符在我的C#程序中不起作用

时间:2018-11-07 20:07:11

标签: c# sql sql-server ssms

我已经在大家的帮助下更新了代码,但是仍然存在'*'通配符不适用于我的程序的问题:

我有一个C#程序,该程序具有一个由输入字段赋予它的单词数组。

我可以在SQL Management Studio中使用(*)通配符运行以下查询,而不会出现问题,并且得到所需的结果:

SELECT Site, StreetAddress, City, State, Zip, PharmacyPhone, MDVersion, 
InstallDate, SiteCodes, SiteNotActive, CloseDate, SiteNotes 
FROM Sites 
WHERE contains(site, '"ev*"' ) OR contains  (StreetAddress, '"ev*"') 
OR contains(city, '"ev*"')

但是,当尝试在我的C#中运行以下代码时,没有得到任何结果。我究竟做错了什么?该数组将包含字符串“ ev *”。 C#代码:

private void btnSearch_Click(object sender, EventArgs e)
    {
        //Get the value from textbox
        string keyword = txtboxKeyword.Text;

        string[] words = keyword.Split(' ');


        try
        {
            switch (words.Length)
            {
                case 1:
                    var select = "SELECT Site, StreetAddress, City, State, Zip, PharmacyPhone, MDVersion, InstallDate, SiteCodes, SiteNotActive, CloseDate, SiteNotes 
                                  FROM Sites WHERE contains(site, @words0) OR contains (StreetAddress, @words0) OR contains(city, @words0)";

                    var conn = new SqlConnection(myconnstr);
                    SqlCommand cmd = new SqlCommand(select, conn);                            
                    cmd.Parameters.AddWithValue("@words0", words[0]);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    var commandbuilder = new SqlCommandBuilder(sda);
                    var dt = new DataTable();
                    sda.Fill(dt);

                    dataGridSites.ReadOnly = true;
                    dataGridSites.DataSource = dt;
                    dataGridSites.CurrentCell = null;                                                                 
                    break;

0 个答案:

没有答案