MySQL Stored procedures:
First, execute a stored procedure in the database:
To create a stored procedure named Gettest
Create PROCEDURE gettest ( in xusername varchar ( xpassword varchar from tb_user WHERE username =
Parameter description: In is the incoming parameter, out is the output parameter. And a inout not used.
The SQL statement inside
After execution succeeds, it can be called by call. See if it's successful
Call Gettest ("admin", @password);
Select @password;
This is called for my stored procedure.
After the success, you can go to the VS inside application.
/// <summary> ///Storage Structure///Create PROCEDURE gettest (in xusername varchar (+), out Xpassword varchar ( +))///BEGIN///SELECT password into Xpassword from Tb_user WHERE username = xusername; ///End/// </summary> /// <param name= "username" ></param> /// <returns></returns> Publicstring GetPassword (string username) {//return value stringPassword =NULL; //connecting to a databaseMysqlconnection Mysqlcon = This. Getconn (); Mysqlcon. Open (); Mysqlcommand Mysqlcommand=NewMysqlcommand (); Mysqlcommand.connection=Mysqlcon; Mysqlcommand.commandtext="gettest";
or "Gettest" is the corresponding stored procedure name. Mysqlcon for database connections
//Mysqlcommand Mysqlcommand = new Mysqlcommand ("Gettest", Mysqlcon);
//Executing stored proceduresMysqlcommand.commandtype =CommandType.StoredProcedure; //set parameter passed in parameter MySQL for? SQL is @Mysqlparameter Username_parameter =NewMysqlparameter ("? xusername", Mysqldbtype.varchar, -);
Assign a value of Username_parameter. Value=username; MYSQLCOMMAND.PARAMETERS.ADD (Username_parameter); Mysqlparameter Password_parameter=NewMysqlparameter ("? Xpassword", Mysqldbtype.varchar, -); MYSQLCOMMAND.PARAMETERS.ADD (Password_parameter);
The output parameter gets the method Password_parameter. Direction=ParameterDirection.Output;
Executive Mysqlcommand.executenonquery (); Password=Password_parameter. Value.tostring (); returnpassword; }
A simple stored procedure is completed for all steps.
C#mysql Stored procedures and applications