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:
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
. Now I want to execute the stored procedure 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?
You can try this.
SqlParameter[] paramCollection = new SqlParameter[1];
SqlParameter param1 = new SqlParameter("name", typeof(string));
paramCollection[0] = param1;
modifying("methodname", paramCollection);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.