我正在调用以下代码。
在第(IDataReader dr = cmd.ExecuteReader())行,SQL出现错误:“Incorrect syntax near 'CompanyUpdate'”
using (SqlCommand cmd = new SqlCommand("CompanyUpdate"))
{
cmd.Parameters.Add("@CompanyID",SqlDbType.Int);
cmd.Parameters.Add("@Description",SqlDbType.VarChar,50);
cmd.Parameters["@CompanyID"].Value = companyid;
cmd.Parameters["@Description"].Value = description;
SqlConnection cn = new SqlConnection("Data Source=[datasource];Initial Catalog=dotNext;User ID=[user];Password=[password];Pooling=True;Application Name=dotNext");
cn.Open();
cmd.Connection = cn;
using (IDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
this.CompanyID = dr.GetInt32(0);
}
}
}
我查看了SqlProfiler并注意到以下内容:
exec sp_executesql N'CompanyUpdate',N'@CompanyID int,@Description varchar(50)',@CompanyID=56,@Description='APC'
它将我的命令包装在 sp_executesql 中。 我的所有其他 SQL 命令都可以正常执行。
所以我的问题有两个方面: 1. 为什么要使用 sp_executesql? 2. 我做错了什么?
详细信息:sql2005,c#,vs2005