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

我可以在我的程序中进行删除,插入和更新,并尝试通过调用从数据库创build的存储过程进行插入。

这个button插入我做得很好。

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

这是调用名为sp_Add_contact的过程来添加联系人的button的开始。 sp_Add_contact(@FirstName,@LastName)的两个参数。 我在谷歌search一个很好的例子,但我没有发现什么有趣的。

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

这与运行查询几乎相同。 在您的原始代码中,您正在创build一个命令对象,将其放入cmdvariables中,而不会使用它。 但是,在这里,您将使用该代替da.InsertCommand

此外,使用所有的一次性物品,以确保它们妥善处置:

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

您必须添加参数,因为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); cmd.Parameters.AddWithValue("@LastName", txtlastname); con.Open(); cmd.ExecuteNonQuery(); } } 

cmd.Parameters.Add(String parameterName, Object value)现在已经被弃用了。 而是使用cmd.Parameters.AddWithValue(String parameterName, Object value)

Add(String parameterName,Object value)已被弃用。 使用AddWithValue(String parameterName,Object value)

在function方面没有区别。 他们之所以弃用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(); } } } 

分别添加参数给我的问题,所以我做到了这一点,它的工作很好:

  string SqlQ = string.Format("exec sp_Add_contact '{0}', '{1}'", txtFirstName.Text, txtLastName.Text); using (SqlConnection con = new SqlConnection(dc.Con)) { using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) { con.Open(); cmd.ExecuteNonQuery(); } } 

另外,我有一个库,可以很容易地使用procs: https : //www.nuget.org/packages/SprocMapper/

 SqlServerAccess sqlAccess = new SqlServerAccess("your connection string"); sqlAccess.Procedure() .AddSqlParameter("@FirstName", SqlDbType.VarChar, txtFirstName.Text) .AddSqlParameter("@FirstName", SqlDbType.VarChar, txtLastName.Text) .ExecuteNonQuery("StoreProcedureName"); 
 public void myfunction(){ try { sqlcon.Open(); SqlCommand cmd = new SqlCommand("sp_laba", sqlcon); cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { sqlcon.Close(); } } 

.NET数据提供程序由许多用于连接数据源,执行命令和返回logging集的类组成。 ADO.NET中的命令对象提供了许多可用于以各种方式执行SQL查询的Execute方法。

存储过程是一个预编译的可执行对象,它包含一个或多个SQL语句。 在许多情况下,存储过程接受input参数并返回多个值。 如果存储过程被写入接受它们,则可以提供参数值。 接受input参数的示例存储过程如下所示:

  CREATE PROCEDURE SPCOUNTRY @COUNTRY VARCHAR(20) AS SELECT PUB_NAME FROM publishers WHERE COUNTRY = @COUNTRY GO 

上面的存储过程是接受一个国家名称(@COUNTRY VARCHAR(20))作为参数,并返回来自input国家的所有发布者。 一旦CommandType设置为StoredProcedure,您可以使用Parameters集合来定义参数。

  command.CommandType = CommandType.StoredProcedure; param = new SqlParameter("@COUNTRY", "Germany"); param.Direction = ParameterDirection.Input; param.DbType = DbType.String; command.Parameters.Add(param); 

上面的代码将国家参数从C#应用程序传递给存储过程。

 using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection connection ; SqlDataAdapter adapter ; SqlCommand command = new SqlCommand(); SqlParameter param ; DataSet ds = new DataSet(); int i = 0; connetionString = "Data Source=servername;Initial Catalog=PUBS;User ID=sa;Password=yourpassword"; connection = new SqlConnection(connetionString); connection.Open(); command.Connection = connection; command.CommandType = CommandType.StoredProcedure; command.CommandText = "SPCOUNTRY"; param = new SqlParameter("@COUNTRY", "Germany"); param.Direction = ParameterDirection.Input; param.DbType = DbType.String; command.Parameters.Add(param); adapter = new SqlDataAdapter(command); adapter.Fill(ds); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show (ds.Tables[0].Rows[i][0].ToString ()); } connection.Close(); } } }