C# 参数附近的语法不正确

C# 参数附近的语法不正确,c#,sql-server,C#,Sql Server,我正在使用Visual Studio 2013、C和SQL Server数据库 如果我用具体的值替换参数,T-SQL命令就可以正常工作 我在最后一行代码中遇到此错误: “@Collection1”附近的语法不正确 我的代码: string myCommandString = "select Name, Collection, Text from List_Card @Collection1"; SqlConnection myConnection = new SqlConnection(conn

我正在使用Visual Studio 2013、C和SQL Server数据库

如果我用具体的值替换参数,T-SQL命令就可以正常工作

我在最后一行代码中遇到此错误:

“@Collection1”附近的语法不正确

我的代码:

string myCommandString = "select Name, Collection, Text from List_Card @Collection1";
SqlConnection myConnection = new SqlConnection(connectionstring);

SqlCommand myCommand = new SqlCommand(myCommandString, myConnection);
SqlDataAdapter myydata = new SqlDataAdapter();

if (comboBox1.Text != "")
{
    string1 = "where Collection IN (select Shortcut from Collections where Collection Like '" + comboBox1.Text + "')";
}
else 
{
    string1 = ""; 
}

myCommand.Parameters.Add(new SqlParameter("@Collection1", string1));
myydata.SelectCommand = myCommand;

myConnection.Open();
DataTable myytab = new DataTable();
myydata.Fill(myytab);

不能通过参数指定整个WHERE子句。允许使用参数代替。。。嗯,参数。如果要根据C代码中的条件添加WHERE子句,请执行以下操作:

string myCommandString = "SELECT Name, Collection, Text FROM List_Card";
.
.
.

if (comboBox1.Text != "")
{
    myCommandString += " WHERE Collection IN (SELECT Shortcut FROM Collections WHERE Collection Like '" + comboBox1.Text + "')";
}
此外,在使用对象后,对其进行恢复是非常重要的。最快的方法是在using语句中关闭代码。最后,你应该尽你最大的努力来预防。在ADO.NET中,向动态SQL查询中添加参数是正确的方法

由于SqlConnection、SqlCommand和SqlDataAdapter都是IDisposable对象,因此您的代码应该如下所示:

string myCommandString = "SELECT Name, Collection, Text FROM List_Card";

using (var  myConnection = new SqlConnection(connectionstring))
using (var  myCommand = new SqlCommand(myCommandString, myConnection))
using (var  myydata = new SqlDataAdapter())
{
    if (comboBox1.Text != "")
    {
        myCommandString += " WHERE Collection IN (SELECT Shortcut FROM Collections WHERE Collection Like @Collection1)";
        myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text));
    }

    myydata.SelectCommand = myCommand;
    myConnection.Open();
    DataTable myytab = new DataTable();
    myydata.Fill(myytab);
}
string myCommandString = @"select Name, Collection, Text from 
List_Card where Collection IN 
    (select Shortcut from Collections where Collection Like '%' + @collection + '%')";
SqlConnection myConnection = new SqlConnection(connectionstring);
SqlCommand myCommand = new SqlCommand(myCommandString, myConnection);
SqlDataAdapter myydata = new SqlDataAdapter();
myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text));
myydata.SelectCommand = myCommand;
myConnection.Open();
DataTable myytab = new DataTable();
myydata.Fill(myytab);

参数不是这样工作的。我猜您希望使用相同的查询,然后在用户选择某个内容时动态添加where子句。不幸的是,您不能以整个where子句都是参数的方式来完成它。您可以尝试以下方法:

string myCommandString = "SELECT Name, Collection, Text FROM List_Card";

using (var  myConnection = new SqlConnection(connectionstring))
using (var  myCommand = new SqlCommand(myCommandString, myConnection))
using (var  myydata = new SqlDataAdapter())
{
    if (comboBox1.Text != "")
    {
        myCommandString += " WHERE Collection IN (SELECT Shortcut FROM Collections WHERE Collection Like @Collection1)";
        myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text));
    }

    myydata.SelectCommand = myCommand;
    myConnection.Open();
    DataTable myytab = new DataTable();
    myydata.Fill(myytab);
}
string myCommandString = @"select Name, Collection, Text from 
List_Card where Collection IN 
    (select Shortcut from Collections where Collection Like '%' + @collection + '%')";
SqlConnection myConnection = new SqlConnection(connectionstring);
SqlCommand myCommand = new SqlCommand(myCommandString, myConnection);
SqlDataAdapter myydata = new SqlDataAdapter();
myCommand.Parameters.Add(new SqlParameter("@Collection1", comboBox1.Text));
myydata.SelectCommand = myCommand;
myConnection.Open();
DataTable myytab = new DataTable();
myydata.Fill(myytab);

您的代码中有几个错误

首先,您的myCommand字符串:

"select Name, Collection, Text from List_Card @Collection1"
这是无效的SQL语法,您得到的错误是什么。您没有对参数做任何操作。您需要将其作为WHERE子句的一部分,但您没有使用该值

接下来,您完全错误地使用了SqlParameter。看看如何正确使用它。具体的问题是没有将条件SQL字符串指定为第二个参数。您需要有条件地将其附加到查询中

最后,您还应该使用语句将所有内容包装起来,以正确处理对象

这将为您提供您想要的:

var myCommandString = "select Name, Collection, Text from List_Card ";

if (comboBox1.Text != "")
{
    myCommandString += " where Collection IN (select Shortcut from Collections where Collection Like '@Collection1')";
}

using (var myConnection = new SqlConnection(connectionstring))
using (var myCommand = new SqlCommand(myCommandString, myConnection))
{
    myCommand.Parameters.Add(new SqlParameter("@Collection1", string1));

    using (var myData = new SqlDataAdapter()) 
    {
        myData.SelectCommand = myCommand;
        myConnection.Open();

        var myytab = new DataTable();
        myydata.Fill(myytab);
    }
}

这绝对是无效的语法字符串myCommandString=从列表中选择名称、集合、文本\u Card@Collection1;在表名后面加上@Collection1,你打算做什么?T-SQL不是数据库引擎我在@Collection中有where子句。若用户在组合框中选择了某个内容,我需要那个“where”子句。如果用户没有在comboBox中选择任何内容,我不需要where子句。参数用于表示数据库引擎的值。您似乎认为参数替代了命令文本中的字符串。这是一个常见的误解。所讨论的数据库引擎是Microsoft SQL Server,而T-SQL或Transact-SQL是SQL Server用于查询数据库引擎的SQL方言,编写函数和过程这是字符串连接,易于SQL注入。请不要鼓励使用不安全和糟糕的方式进行查询。这是OP已经具备的。SQL语句的组合不是那篇文章的主题,是吗?跳过IDisposable对象的处理将导致应用程序不稳定,因此,请不要鼓励这样做。