调用存储过程在C#中的参数 [英] Call a stored procedure with parameter in c#

查看:116
本文介绍了调用存储过程在C#中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以做删除,插入和更新在我的计划,我尝试做通过呼叫创建存储过程插入从我的数据库。

这一个按钮插入我做工作做好。

 私人无效btnAdd_Click(对象发件人,EventArgs的)
    {
        SqlConnection的CON =新的SqlConnection(dc.Con);
        的SqlCommand CMD =新的SqlCommand(字符串命令,CON);

        da.InsertCommand =新的SqlCommand(INSERT INTO tblContacts VALUES(@名字,@姓氏),CON);
        da.InsertCommand.Parameters.Add(@名字,SqlDbType.VarChar)。价值= txtFirstName.Text;
        da.InsertCommand.Parameters.Add(@名字,SqlDbType.VarChar)。价值= txtLastName.Text;

        con.Open();
        da.InsertCommand.ExecuteNonQuery();
        con.Close();
        dt.Clear();
        da.Fill(DT);
    }
 

这是按钮开始调用一个名为 sp_Add_contact 程序添加联系人。这两个参数 sp_Add_contact(@名字,@姓氏)。我搜索的谷歌的一些很好的例子,但我发现没有什么有趣的。

 私人无效的button1_Click(对象发件人,EventArgs的)
    {
        SqlConnection的CON =新的SqlConnection(dc.Con);
        的SqlCommand CMD =新的SqlCommand(字符串命令,CON);
        cmd.CommandType = CommandType.StoredProcedure;
        ???

        con.Open();
        DA。 ???的ExecuteNonQuery()。
        con.Close();
        dt.Clear();
        da.Fill(DT);
    }
 

解决方案

这是pretty的大致相同运行查询。在原来的code要创建一个命令对象,将它放在 CMD 变量,并且从来不使用它。但在这里,你会用这个来代替 da.InsertCommand

此外,使用使用所有可支配的对象,让你确信他们得到适当的处理:

 私人无效的button1_Click(对象发件人,EventArgs的){
  使用(SqlConnection的CON =新的SqlConnection(dc.Con)){
    使用(CMD的SqlCommand =新的SqlCommand(sp_Add_contact,CON)){
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add(@名字,SqlDbType.VarChar)。价值= txtFirstName.Text;
      cmd.Parameters.Add(@名字,SqlDbType.VarChar)。价值= txtLastName.Text;

      con.Open();
      cmd.ExecuteNonQuery();
    }
  }
}
 

I can do a Delete, Insert and Update in my program and I try to do an insert by call a created Store Procedure from my Database.

This a button insert I make work well.

  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);
    } 

This is the start of the button to call the procedure named sp_Add_contact to add a contact. The two parameters for sp_Add_contact(@FirstName,@LastName). I searched on google for some good example but I found nothing interesting.

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);
    }

解决方案

It's pretty much the same as running a query. In your original code you are creating a command object, putting it in the cmd variable, and never use it. Here, however, you will use that instead of da.InsertCommand.

Also, use a using for all disposable objects, so that you are sure that they are disposed properly:

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();
    }
  }
}

这篇关于调用存储过程在C#中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆