[英]Passing SqlParameter[] to a method
I want to make a method that execute a stored procedure whose name is a parameter of the method, and the parameters (variables) of the stored procedure are provided by SqlParameter[] param
as follows: 我想制作一个执行存储过程的方法,该存储过程的名称是该方法的参数,而该存储过程的参数(变量)由
SqlParameter[] param
,如下所示:
public void modifying(string method, SqlParameter[] param)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-HP5H4JL\SQLEXPRESS;Initial Catalog=Pocket Money;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = method;
cmd.Connection = con;
if (param != null)
cmd.Parameters.AddRange(param);
cmd.ExecuteNonQuery();
}
This method is in the class Modify
. 此方法在
Modify
类中。 Now I want to execute the stored procedure insertItemStudents
: 现在,我要执行存储过程
insertItemStudents
:
CREATE PROCEDURE insertItemStudents
@name VARCHAR(50),
@class VARCHAR(10),
@Gender BIT,
@BirthDate DATE,
@PhoneNumber CHAR(11),
@Email VARCHAR(50)
AS
INSERT INTO Students( StudentName, Class, Gender, BirthDate, PhoneNumber, Email)
VALUES (@name, @class, @Gender, @BirthDate, @PhoneNumber, @Email)
RETURN 0
I created a form in which there are text boxes to insert the new values. 我创建了一个表单,其中有一些文本框可用于插入新值。 But the problem is: how to pass the
SqlParameter[]
as an argument? 但是问题是:如何将
SqlParameter[]
作为参数传递?
You can try this. 你可以试试看
SqlParameter[] paramCollection = new SqlParameter[1];
SqlParameter param1 = new SqlParameter("name", typeof(string));
paramCollection[0] = param1;
modifying("methodname", paramCollection);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.