When calling a stored procedure, two conditions are involved: one is execution without the need to return values, such as deletion or modification; the other is execution and requires a return value, such as a query. When calling a stored procedure in C #, two types of SqlCommand and SqlDataAdapter are mainly used. The CommandType attribute of the SqlCommand class can be used to obtain or set the Transact-SQL statement or stored procedure to be executed on the data source. When SqlCommand is associated with a stored procedure, it can be executed through the SqlCommand class. Another method is to use the Fill method of the SqlDataAdapter class. The Fill method of the SqlDataAdapter class not only executes the stored procedure, but also puts the result into the result set.
In Example C #, call the stored procedure to create a stored procedure (Proc_InsertOfPelple) for adding information and a stored procedure for searching information (Proc_SelectOfPelple ). Then define a data operation class (ClsDBProcControl) to implement various operations on the table (t_People). The ClsDBProcControl class includes the ConDB method, Insert_Proc method, select_Proc method, and the entity class corresponding to the t_People table, when the user clicks the add information button while the program is running, the name and gender information are assigned to the properties of the t_People table, and new information is inserted into the data table by calling the Insert_Proc method, when you click Query Information, assign the name and gender information to the properties of the t_People table, and then call the select_Proc method to query the specified information.
The program code is as follows.
When you click Add information, first assign the name and gender information to the str_Name and str_Sex attributes, and then call the Insert_Proc method to insert information. The Code is as follows:
Private void button#click (object sender, EventArgs e)
{
ClsDB. ClsDBProcControl CBDP = new OptDB. ClsDB. ClsDBProcControl ();
CBDP. str_Name = this. textBox2.Text. Trim (). ToString ();
CBDP. str_Sex = this. textBox3.Text. Trim (). ToString ();
If (CBDP. Insert_Proc (CBDP ))
{
MessageBox. Show ("inserted successfully ");
CBDP. str_Name = string. Empty;
CBDP. str_Sex = string. Empty;
This. dataGridView1.DataSource = CBDP. select_Proc (CBDP). Tables [0]. DefaultView;
}
} When you click the Query Information Button, first assign the name and gender information to the str_Name and str_Sex attributes, and then call the select_Proc method, the select_Proc method returns a DataSet object for displaying the result to the user. The Code is as follows:
Private void button2_Click (object sender, EventArgs e)
{
ClsDB. ClsDBProcControl CBDP = new OptDB. ClsDB. ClsDBProcControl ();
CBDP. str_Name = this. textBox2.Text. Trim (). ToString ();
CBDP. str_Sex = this. textBox3.Text. Trim (). ToString ();
This. dataGridView1.DataSource = CBDP. select_Proc (CBDP). Tables [0]. DefaultView;
} The t_People table corresponds to an object. The GET and SET accessors are used to define the str_Name and str_Sex attributes. The Code is as follows:
# Region // The object corresponding to the table
Private string strName;
Public string str_Name
{
Get
{
Return this. strName;
}
Set
{
StrName = value;
}
}
Private string strSex;
Public string str_Sex
{
Get
{
& N