我需要一个带参数的更新命令,由于某些原因我不能使用存储过程,实际上我们根据数据库、表和列生成更新命令,我们使用以下形式:
string conStr = "Provider=SQLNCLI10;Server=.\\sql2008;DataBase=MyDataBase;Trusted_Connection=true;";
DbProviderFactory dbFactory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection dbConnection = dbFactory.CreateConnection();
dbConnection.ConnectionString = conStr;
DbCommand dbCommand = dbFactory.CreateCommand();
dbCommand.CommandText = "UPDATE [Student] SET Name = @Name Where Id = @Id";
DbParameter param1 = dbCommand.CreateParameter();
param1.ParameterName = "@Name";
param1.Value = "LOL";
DbParameter param2 = dbCommand.CreateParameter();
param2.ParameterName = "@Id";
param2.Value = 5;
dbCommand.Parameters.Add(param1);
dbCommand.Parameters.Add(param2);
dbConnection.Open();
dbCommand.ExecuteNonQuery();
dbConnection.Close();
但有一个例外:
必须声明标量变量“@Name”
这段代码的问题在哪里?有人对此有任何想法吗?