C # calls the MySQL stored procedure with parameters

Source: Internet
Author: User

1. First create a stored procedure with parameters

① Stored Procedure name =proc_bookinfo

② Stored Procedure 2 parameters one in an out in Parameter name =ispay out parameter name =unpaycount This stored procedure outputs the number of items that have not been paid based on the incoming unpaid field name
DELIMITER $$    Use ' Bookshop ' $$    DROP PROCEDURE IF EXISTS ' proc_bookinfo ' $$    CREATE definer= ' @ ' PROCEDURE ' proc_bookinfo ' (In Ispay VARCHAR (ten), out Unpaycount INT)  BEGIN   SELECT COUNT (*) to Unpaycount from Tb_ BookInfo as a,tb_shopbook as b WHERE b.ispay=ispay;   end$$    

2.c# code calls the stored procedure

The ①sql statement only needs to write the name of the stored procedure

② the name and data type of the 2 parameters and the data length are exactly the same as the parameters of the stored procedure defined in the database (capitalization does not affect)

③ the direction attribute of 2 parameters to the corresponding type

④ call the Excuteproc method to execute, the output value is the second parameter passed in

StringBuilder sql=NewStringBuilder (); Sql. Append ("Proc_bookinfo"); mysqlparameter[] Parameters= {                     NewMysqlparameter ("@ispay", Mysqldbtype.varchar,Ten),                     NewMysqlparameter ("@unPaycount", Mysqldbtype.int32, One)                                           }; parameters[0]. Value ="not paid"; parameters[0]. Direction =ParameterDirection.Input; parameters[1]. Value =0 ; parameters[1]. Direction =ParameterDirection.Output; Dbhelpermysql.executeproc (convert.tostring (SQL), parameters);

3. How to perform database operations

Note: The CommandType property must be assigned a value of CommandType.StoredProcedure

     Public Static ObjectExecuteproc (stringSQLString,paramsmysqlparameter[] cmdparms) {                  using(Mysqlconnection connection =Newmysqlconnection (connectionString)) {                      using(Mysqlcommand cmd =NewMysqlcommand ()) {                          Try{preparecommand (cmd, connection, NULL, SQLString, cmdparms); Cmd.commandtype=CommandType.StoredProcedure; introws =cmd.                              ExecuteNonQuery (); Console.WriteLine ("The return value is"+cmdparms[1].                              Value); Cmd.                              Parameters.clear (); returnrows; }                          Catch(MySql.Data.MySqlClient.MySqlException e) {connection.                              Close (); Throwe; }                      }                  }              }  

C # calls the MySQL stored procedure with parameters

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.