[英]Passing a StoredProcedure with Parameters to a function in c#
如何将带有参数的存储过程传递给函数。
我实际上有一个将数据插入数据库的函数。我正在寻找一种方法,可以将带有参数的SP传递给该插入函数,以便重用插入数据代码。
将对存储过程的调用放在一个方法中,然后从要调用它的任何位置调用该方法。 将参数(字符串,整数,双精度数,...)传递给此方法,并将这些值放入存储过程的参数中。
这样,您可以将所有SP代码保存在一个地方。
public class CustomerProvider
{
public int UpdateCustomer(int id, string name, string address)
{
using(connection = new
SqlConnection("Server=localhost;DataBase=Northwind;Integrated Security=SSPI"))
{
connection.Open();
var command = new SQLCommand("csp_updatecustomer", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(
new SqlParameter("@CustomerID", id));
command.Parameters.Add(
new SqlParameter("@CustomerName", name));
command.Parameters.Add(
new SqlParameter("@CustomerAddress", address));
var result = command.ExecuteNonQuery();
return result;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.