使用c#中的参数调用存储过程

Fra*_*arp 132 c# sql-server asp.net ado.net stored-procedures

我可以在我的程序中执行删除,插入和更新,并尝试通过从我的数据库中调用创建的存储过程来执行插入操作.

这个按钮插件我工作得很好.

private void btnAdd_Click(object sender, EventArgs e)
{
        SqlConnection con = new SqlConnection(dc.Con);
        SqlCommand cmd = new SqlCommand("Command String", con);

        da.InsertCommand = new SqlCommand("INSERT INTO tblContacts VALUES (@FirstName, @LastName)", con);
        da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
        da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;

        con.Open();
        da.InsertCommand.ExecuteNonQuery();
        con.Close();

        dt.Clear();
        da.Fill(dt);
    } 
Run Code Online (Sandbox Code Playgroud)

这是调用名为sp_Add_contact添加联系人的过程的按钮的开始.这两个参数sp_Add_contact(@FirstName,@LastName).我在谷歌搜索了一些很好的例子,但我没有发现任何有趣的东西.

private void button1_Click(object sender, EventArgs e)
{
        SqlConnection con = new SqlConnection(dc.Con);
        SqlCommand cmd = new SqlCommand("Command String", con);
        cmd.CommandType = CommandType.StoredProcedure;

        ???

        con.Open();
        da. ???.ExecuteNonQuery();
        con.Close();

        dt.Clear();
        da.Fill(dt);
    }
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 249

它与运行查询几乎相同.在原始代码中,您正在创建一个命令对象,将其放入cmd变量中,并且永远不会使用它.但是,在这里,您将使用它而不是da.InsertCommand.

此外,using对所有一次性物品使用a ,以确保它们处理得当:

private void button1_Click(object sender, EventArgs e) {
  using (SqlConnection con = new SqlConnection(dc.Con)) {
    using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
      cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;

      con.Open();
      cmd.ExecuteNonQuery();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @ M009:然后使用`ExecuteReader`或`ExecuteScalar`来调用它. (8认同)
  • 但如果此过程返回数据,我怎样才能在C#中捕获它? (6认同)
  • @ M009:是的,这是做同样事情的另一种方式.数据适配器使用`ExecuteReader`. (2认同)

Rav*_*dag 35

您必须添加参数,因为SP需要执行

using (SqlConnection con = new SqlConnection(dc.Con))
{
    using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
        cmd.Parameters.AddWithValue("@LastName", txtlastname.Text);
        con.Open();
        cmd.ExecuteNonQuery();
    }            
}
Run Code Online (Sandbox Code Playgroud)

  • AddWithValue是个坏主意; SQL Server并不总是使用nvarchar或varchar的正确长度,从而导致发生隐式转换.最好明确指定参数的长度,然后使用`parameter.Value = txtfirstname`分别添加值. (6认同)

Rah*_*ate 13

cmd.Parameters.Add(String parameterName, Object value)现已弃用.而是使用cmd.Parameters.AddWithValue(String parameterName, Object value)

不推荐使用Add(String parameterName,Object value).使用AddWithValue(String parameterName,Object value)

功能方面没有区别.他们弃用cmd.Parameters.Add(String parameterName, Object value)赞成的原因AddWithValue(String parameterName, Object value)是为了更清晰.这是相同的MSDN参考

private void button1_Click(object sender, EventArgs e) {
  using (SqlConnection con = new SqlConnection(dc.Con)) {
    using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
      cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;

      con.Open();
      cmd.ExecuteNonQuery();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @TonyG:这不是真的,接受的答案使用了`Add`的首选重载,也没有弃用.`AddWithValue`也不是最好的方法,因为它从参数值中推断出参数的类型.这通常会导致糟糕的执行计划或错误的转换.它也不首先验证参数(fe类型,如果`Datetime`但你传递`String`).你可以看到[这里](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.add(v = vs.110).aspx)只有`添加'需要一个不推荐使用`Object`作为第二个参数. (6认同)
  • 您是否拥有不推荐使用“ cmd.Parameters.Add”声明的链接或来源? (2认同)
  • `AddWithValue`与'添加'与'对象'具有相同的功能,但它不是首选的方式.两者都需要推断出类型. (2认同)
  • @TimSchmelter,您绝对正确。我对文本的阅读存在缺陷。感谢您的更正。我正在编写一些将要使用Add()的新代码。我会根据这个答案将我的赞成票改为反对票,因为Rahul Nikate和我一样被误解了。 (2认同)
  • @TimSchmelter谢谢您的建议。我已经编辑了答案。 (2认同)