System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.'关于数据表和对象

System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' on Datatable and object

我在本网站和其他地方看过很多类似的问题,但 none 对我有帮助。

我正在尝试通过查询建立数据库连接,但出现错误

System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.'

在 2 行不同的代码上。我尝试在 = 周围的查询中使用空格,但这没有帮助。

代码 1 是:

string connectieString = dbConnection();

SqlConnection connection = new SqlConnection(connectieString);

SqlCommand select = new SqlCommand();
select.Connection = connection;

select.Parameters.Add("@attackCategory", SqlDbType.NChar).Value = attackCategory;
select.Parameters.Add("@taughtOn", SqlDbType.NVarChar).Value = taughtOn;
select.CommandText = "SELECT ID, Name FROM attackCategory = @attackCategory WHERE TaughtOn = @taughtOn";

using (SqlDataAdapter sda = new SqlDataAdapter(select.CommandText, connection))
{
    DataTable dt = new DataTable();
    sda.Fill(dt);

    return dt;
}

在代码的 sda.Fill(dt); 行抛出异常。如果查询中未使用任何参数,则此代码有效:

string cmd = @"select ID, Name from " + attackCategory + " where TaughtOn ='" + taughtOn + "'";

而代码 2 是:

string connectieString = dbConnection();

SqlConnection connection = new SqlConnection(connectieString);

SqlCommand select = new SqlCommand();
select.Connection = connection;

select.Parameters.Add("@attackCategory", SqlDbType.NVarChar).Value = attackCategory;
select.Parameters.Add("@ID", SqlDbType.Int).Value = id;
select.CommandText = "SELECT Name FROM attackCategory = @attackCategory WHERE ID = @ID";

connection.Open();
object name = select.ExecuteScalar();
connection.Close();

return name;

异常在代码的 object name = select.ExecuteScalar(); 行触发。如果在查询中使用了 1 个参数,则此代码有效:

select.Parameters.Add("@ID", SqlDbType.Int).Value = id;
select.CommandText = "SELECT Inhabitants FROM Planet WHERE ID=@ID";

您不能提供 table 名称有参数,参数适用于具有列值的 where 子句。

string cmd = @"select ID, Name from " + attackCategory + " where TaughtOn ='" + taughtOn + "'";

但是,我们需要简化以在此查询中使用参数。

SqlCommand select = new SqlCommand();
 select.Connection = connection;
 select.Parameters.Add("@taughtOn", SqlDbType.VarChar,50).Value = taughtOn;
 string cmd = @"select ID, Name from " + attackCategory + " where TaughtOn  =@taughtOn"; 
 select.CommandText = cmd;

在上面的 tsql 查询中,应用了字符串连接并且 table 名称包含在字符串中,这将起作用。

编辑:-

我明白为什么 sqlDataAdapter 无法识别参数。 原因是你没有提供。是的,您提供的是 CommandText 而不是 select 变量的命令对象。

我已更正您的代码。

select.Parameters.Add("@taughtOn", SqlDbType.VarChar, 50).Value = taughtOn; 
string cmd = @"select ID, Name from " + attackCategory + " where TaughtOn =@taughtOn"; 
select.CommandText = cmd; 
select.Connection = new SqlConnection("provide your sql string");
using (SqlDataAdapter sda = new SqlDataAdapter(select)) 
{ 
DataTable dt = new DataTable(); 
sda.Fill(dt); 
return dt; 
}

希望对您有所帮助!!

您不能那样绑定对象名称。对于对象名称,您将不得不求助于某种字符串连接。例如:

select.Parameters.Add("@taughtOn", SqlDbType.NVarChar).Value = taughtOn;
select.CommandText = "SELECT ID, Name FROM " + attackCategory + " WHERE TaughtOn=@taughtOn";

注:
这是一个过于简化的解决方案,无法降低 SQL-注入攻击的风险。在像这样使用它之前,您需要先清理 attackCategory