简体   繁体   English

查看我的C#SQLite查询,我做错了什么?

[英]Look over my C# SQLite Query, what am I doing wrong?

I'm writing a WinForms database application using SQLite and C#. 我正在使用SQLite和C#编写WinForms数据库应用程序。 I have a sqlite query that is failing, and I'm unsure as to where I'm going wrong, as I've tried everything I could think of. 我有一个sqlite查询失败了,我不确定我要去哪里,因为我已经尝试了所有我能想到的东西。

public DataTable searchSubs(String businessName, String contactName)
    {
        string SQL = null;

        if ((businessName != null && businessName != "") && (contactName != null && contactName != ""))
        {
            // provided business name and contact name for search
            SQL = "SELECT * FROM SUBCONTRACTOR WHERE BusinessName LIKE %@BusinessName% AND Contact LIKE %@ContactName%";
        }
        else if ((businessName != null && businessName != "") && (contactName == null || contactName == ""))
        {
            // provided business name only for search
            SQL = "SELECT * FROM SUBCONTRACTOR WHERE BusinessName LIKE %@BusinessName%";
        }
        else if ((businessName == null || businessName == "") && (contactName != null && contactName != ""))
        {
            // provided contact name only for search
            SQL = "SELECT * FROM SUBCONTRACTOR WHERE Contact LIKE %@ContactName%";
        }
        else if ((businessName == null || businessName == "") && (contactName == null || contactName == ""))
        {
            // provided no search information
            SQL = "SELECT * FROM SUBCONTRACTOR";
        }
        SQLiteCommand cmd = new SQLiteCommand(SQL);
        cmd.Parameters.AddWithValue("@BusinessName", businessName);
        cmd.Parameters.AddWithValue("@ContactName", contactName);
        cmd.Connection = connection;
        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            return dt;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            return null;
        }
        finally
        {
            cmd.Dispose();
            connection.Close();
        }
    }

I continually get an error saying that it is failing near the %'s. 我不断收到错误消息,说它在%的附近失败。 That's all fine and dandy, but I guess I'm structuring it wrong, but I don't know where! 一切都很好,但我想我把它的结构弄错了,但我不知道在哪里! I tried adding apostrophes around the "like" variables, like this: 我尝试在“喜欢”变量周围添加撇号,如下所示:

SQL = "SELECT * FROM SUBCONTRACTOR WHERE Contact LIKE '%@ContactName%'";

and quite honestly, that is all I can think of. 老实说,这就是我所能想到的。 Anyone have any ideas? 有人有想法么?

Because you are using the parameter value you don't need the '%'. 因为您使用的是参数值,所以不需要'%'。 so you should have something like 所以你应该有类似

SQL = "SELECT * FROM SUBCONTRACTOR WHERE Contact LIKE @ContactName "; SQL = "SELECT * FROM SUBCONTRACTOR WHERE Contact LIKE @ContactName ”;

the '%' will need to be added to the value that is passed to the parameter 必须将'%'添加到传递给参数的值中

var contactNameSearch = string.Format("{0}%", contactName);
cmd.Parameters.AddWithValue("@ContactName", contactNameSearch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM