C # Call the MySQL Stored Procedure

Source: Internet
Author: User

SQL code
Create procedure 'yuan _ user_cc '(in xname varchar (100), in xemail varchar (100), in xpassword varchar (100), out xid integer)
Begin
Insert into yuan_user (name, email, password) values (xname, xemail, xpassword );
Select id into xid from yuan_user where name = xname;
End
 
C # code
Private string shuju_lianjie (string name, string email, string password)
{

Int16 id; // The returned id.
MySqlConnection mysqlcon;
MySqlDataAdapter mysqldata = new MySqlDataAdapter ();
DataSet dataset = new DataSet ();
Mysqlcon = new MySqlConnection ("server = localhost; user id = root; password = 123; database = yuan_test; pooling = false; charset = gb2312 ");
Mysqldata. SelectCommand = new MySqlCommand ();
Mysqldata. SelectCommand. Connection = mysqlcon;
Mysqldata. SelectCommand. CommandText = "yuan_user_cc ";
Mysqldata. SelectCommand. CommandType = CommandType. StoredProcedure;
// Set parameters and add them to the database
MySqlParameter name_parameter = new MySqlParameter ("? Xname ", MySqlDbType. VarChar, 100); // What is the Stored Procedure Parameter of mysql? Headers !!!!
Name_parameter.Value = name;
Mysqldata. SelectCommand. Parameters. Add (name_parameter );
// ++ ++
MySqlParameter email_parameter = new MySqlParameter ("? Xemail ", MySqlDbType. VarChar, 100); // What is the Stored Procedure Parameter of mysql? Headers !!!!
Email_parameter.Value = email;
Mysqldata. SelectCommand. Parameters. Add (email_parameter );
// ++ ++
MySqlParameter password_parameter = new MySqlParameter ("? Xpassword ", MySqlDbType. VarChar, 100); // What is the Stored Procedure Parameter of mysql? Headers !!!!
Password_parameter.Value = password;
Mysqldata. SelectCommand. Parameters. Add (password_parameter );
// ++ Return value ++ ++
MySqlParameter id_parameter = new MySqlParameter ("? Xid ", MySqlDbType. Int16, 15); // What is the Stored Procedure Parameter of mysql? Headers !!!!
Id_parameter.Direction = ParameterDirection. Output;
Mysqldata. SelectCommand. Parameters. Add (id_parameter );
// ++ Open the database and insert data, returns the id value +++ ++
Try
{
Mysqldata. Fill (dataset, "yuan_user_cc ");

Id = (Int16) id_parameter.Value;
Return id. ToString ();
}
Catch (MySqlException ex)
{
MessageBox. Show (ex. Message );
Return String. Empty;
}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.