![](/img/trans.png)
[英]'Procedure or function '...' expects parameter '...', which was not supplied.'
[英]“Procedure or function expects parameter which was not supplied.”
我试图执行存储过程并打印输出,但是当我运行下面的代码时,出现诸如“过程或函数'SPInsertLocal'期望未提供参数'@RES'之类的错误”。
private void InsertPdtLocal(string code, string PON,string Qty)
{
string str = Properties.Settings.Default.conLocal;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("Execute SPInsertLocal @PON,@TCode,@Qty,@Type", con);
try
{
con.Open();
cmd.CommandTimeout = 150;
cmd.Parameters.AddWithValue("@PON", PON);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@TCode", code);
cmd.Parameters.AddWithValue("@Type", Globals.s_type);
SqlParameter output = new SqlParameter("@RES", SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
cmd.ExecuteNonQuery();
con.Close();
int id = Convert.ToInt32(output.Value);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我在这里做错了什么?
SqlCommand cmd = new SqlCommand("Execute SPInsertLocal @PON,@TCode,@Qty,@Type,@RES", con);
我没有传递参数,解决了问题
您可以按以下方式重构代码,其中using语句用于自动管理连接关闭,并避免在c#代码中对Execute语句进行硬编码,这是一种不好的做法
private void InsertPdtLocal(string code, string PON,string Qty)
{
string str = Properties.Settings.Default.conLocal;
try
{
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.Parameters.AddWithValue("@PON", PON);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@TCode", code);
cmd.Parameters.AddWithValue("@Type", Globals.s_type);
var output = cmd.Parameters.Add("@RES" , SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int id = Convert.ToInt32(output.Value);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.