asp.net c# mysql存储过程

//asp.net C# 

//myql5.0以上

using System.Data; //CommandType  , ParameterDirection

using System.Diagnostics; //Debug , visual web developer中调试窗口中输出

 

public  void test()
    {
        MySqlConnection conn = new MySqlConnection();
        conn.ConnectionString = "server=localhost;user=root;database=test;port=3306;password=123456;";
        MySqlCommand cmd = new MySqlCommand();

        try
        {
            Debug.WriteLine("Connecting to MySQL...");
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = "DROP PROCEDURE IF EXISTS add_emp";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "DROP TABLE IF EXISTS emp";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "CREATE TABLE emp (empno INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20), birthdate DATE)";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "CREATE PROCEDURE add_emp(" +
                                "IN fname VARCHAR(20), IN lname VARCHAR(20), IN bday DATETIME, OUT empno INT)" +
                                "BEGIN INSERT INTO emp(first_name, last_name, birthdate) " +
                                "VALUES(fname, lname, DATE(bday)); SET empno = LAST_INSERT_ID(); END";

            cmd.ExecuteNonQuery();
        }
        catch (MySqlException ex)
        {
            Console.WriteLine ("Error " + ex.Number + " has occurred: " + ex.Message);
        }
        conn.Close();
        Console.WriteLine("Connection closed.");
        try
        {
            Debug.WriteLine("Connecting to MySQL...");


            conn.Open();
            cmd.Connection = conn;

            cmd.CommandText = "add_emp";
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@lname", "Jones");
            cmd.Parameters["@lname"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@fname", "Tom");
            cmd.Parameters["@fname"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@bday", "1940-06-07");
            cmd.Parameters["@bday"].Direction = ParameterDirection.Input;

            cmd.Parameters.AddWithValue("@empno", MySqlDbType.Int32);
            cmd.Parameters["@empno"].Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();

           
           Debug.WriteLine("Employee number: " + cmd.Parameters["@empno"].Value);
           Debug.WriteLine("Birthday: " + cmd.Parameters["@bday"].Value);
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            Debug.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
        }
        conn.Close();
        Debug.WriteLine("Done.");

}

//上面代码来自 Mysql.Data.chm

程序输出:

Employee number: 1
Birthday: 1940-06-07

你可能感兴趣的:(asp.net c# mysql存储过程)