mysql儲存過程及c#呼叫標準版

iDotNetSpace發表於2009-07-02
private string shuju_lianjie(string name, string email, string password)
        {
            //////////////////////////////////////////////////////////////////////////////
            ///獲取使用者名稱、郵件及設定密碼,增加到資料庫的使用者表後返回該使用者註冊id值,不考慮 安全,僅僅表示程式碼的寫作範例是否合乎要求
            ///儲存過程為:
            ///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
            //////////////////////////////////////////////////////////////////////////////
            Int16 id;//返回的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;
            //設定引數,新增到資料庫
            MySqlParameter name_parameter = new MySqlParameter("?xname", MySqlDbType.VarChar,100);//mysql的儲存過程引數是以?打頭的!!!!
            name_parameter.Value = name;
            mysqldata.SelectCommand.Parameters.Add(name_parameter);
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            MySqlParameter email_parameter = new MySqlParameter("?xemail", MySqlDbType.VarChar, 100);//mysql的儲存過程引數是以?打頭的!!!!
            email_parameter.Value = email;
            mysqldata.SelectCommand.Parameters.Add(email_parameter);
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            MySqlParameter password_parameter = new MySqlParameter("?xpassword", MySqlDbType.VarChar, 100);//mysql的儲存過程引數是以?打頭的!!!!
            password_parameter.Value = password;
            mysqldata.SelectCommand.Parameters.Add(password_parameter);
            //++++++++++++++++++++++++++返回值++++++++++++++++++++++++++++++++++++++++++
            MySqlParameter id_parameter = new MySqlParameter("?xid", MySqlDbType.Int16,15);//mysql的儲存過程引數是以?打頭的!!!!
            id_parameter.Direction=ParameterDirection.Output;
            mysqldata.SelectCommand.Parameters.Add(id_parameter);
            //+++++++++++++++++++++++++開啟資料庫,插入資料,返回id值+++++++++++++++++++
            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;
            }

        }  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-608173/,如需轉載,請註明出處,否則將追究法律責任。

相關文章