使用Update功能出现:必须声明标量变量@Id

.Net技术 码拜 8年前 (2016-05-19) 1592次浏览
这是一个WinForm项目:具体的三层如下
Dao层 SqlHelper.cs:
public class SqlHelper
{
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
String connStr = ConfigurationManager.ConnectionStrings[“ConnStr”].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
return cmd.ExecuteNonQuery();
}
}
服务层 Service.cs   :
public static int dataUpdate(int i,params Object[] o)
{
String strsql = “Update Device set TName = @TName Where Id = @Id “;
SqlHelper.ExecuteNonQuery(strsql,
new SqlParameter(“@Id”, i),
new SqlParameter(“@TName”, o[0]));
int result = SqlHelper.ExecuteNonQuery(strsql);
return result;
}
主窗体修改按钮:
private void btnUpdate_Click(object sender, EventArgs e)
{
MessageBox.Show(pdtp.Value.ToString(“yyyy/MM/dd”));
Service.dataUpdate(
Convert.ToInt32(textBox1.Text),
NameText.Text
);
}
问!服务层的内容使用Insert就没问题,为什么update就报错了?
解决方案

11

重复执行了两次sql,一次有参,一次无参

11

引用:
引用

        SqlHelper.ExecuteNonQuery(strsql,
new SqlParameter(“@Id”, i),
new SqlParameter(“@TName”, o[0]));
int result = SqlHelper.ExecuteNonQuery(strsql);

这是想干嘛?

同问。直接int result =SqlHelper.ExecuteNonQuery(strsql, new SqlParameter(“@Id”, i), new SqlParameter(“@TName”, o[0]));
不就可以了吗?下面为什么还要再执行一遍没有SqlParameter的方法?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明使用Update功能出现:必须声明标量变量@Id
喜欢 (0)
[1034331897@qq.com]
分享 (0)