无法获取 SQL 命令来识别添加的参数

发布于 2024-08-30 01:44:33 字数 382 浏览 6 评论 0原文

我已经有一段时间没有使用基本的 SQL 命令了,我正在尝试将参数传递给存储过程并运行它。但是,当我运行代码时,出现“未提供”错误。

代码:

SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr());
SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1);
cmd1.Parameters.Add(new SqlParameter("@ID", itemId));
conn1.Open();
cmd1.ExecuteNonQuery();

我不太确定为什么会失败。对于基本问题表示歉意,但我迷路了!

提前致谢。

I've not used basic SQL commands for a while and I'm trying to pass a param to a sproc and the run it. However when I run the code I get a "Not Supplied" error.

Code:

SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr());
SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1);
cmd1.Parameters.Add(new SqlParameter("@ID", itemId));
conn1.Open();
cmd1.ExecuteNonQuery();

I'm not really sure why this would fail. Apologies for the basic question, but I'm lost!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-09-06 01:44:33

您应该将 CommandType 设置为 StoredProcedure,设置连接并使用 Parameters.AddWithValue("@ID", itemID)

cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Connection = conn1;
cmd1.Parameters.AddWithValue("@ID",itemID);
conn1.Open();
cmd1.ExecuteNonQuery();

如果您想使用Parameters.Add() (已过时),这是您的操作方法(您也需要传递类型)

cmd1.Parameters.Add("@ID", SqlDbType.Int); //string maybe, I don't know
cmd1.Parameters["@ID"].Value = itemID;

这应该有效:

SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr());
SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@ID", itemId);
conn1.Open();
cmd1.ExecuteNonQuery();

You should set the CommandType to StoredProcedure, set the connection and use Parameters.AddWithValue("@ID", itemID)

cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Connection = conn1;
cmd1.Parameters.AddWithValue("@ID",itemID);
conn1.Open();
cmd1.ExecuteNonQuery();

If you want to use Parameters.Add() (which is obsolete), here is how you do it (you need to pass the type too)

cmd1.Parameters.Add("@ID", SqlDbType.Int); //string maybe, I don't know
cmd1.Parameters["@ID"].Value = itemID;

This should work:

SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr());
SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@ID", itemId);
conn1.Open();
cmd1.ExecuteNonQuery();
不醒的梦 2024-09-06 01:44:33

为了使您的代码变得更好,请将 SqlConnectionSqlCommand 放入 using 语句中,以便它们在结束时自动释放使用块:

using(SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr()))
{
   using(SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1))
   {
        cmd1.CommandType = CommandType.StoredProcedure;
        cmd1.Parameters.AddWithValue("@ID", itemId);

        conn1.Open();

        cmd1.ExecuteNonQuery();

        conn.Close();
   }
}

And to make your code even better, put your SqlConnection and SqlCommand into using statements, so that they'll be freed automatically at the end of the using block:

using(SqlConnection conn1 = new SqlConnection(DAL.getConnectionStr()))
{
   using(SqlCommand cmd1 = new SqlCommand("SProc_Item_GetByID", conn1))
   {
        cmd1.CommandType = CommandType.StoredProcedure;
        cmd1.Parameters.AddWithValue("@ID", itemId);

        conn1.Open();

        cmd1.ExecuteNonQuery();

        conn.Close();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文