Sql代碼
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#代碼
private string shuju_lianjie(string name, string email, string password)
{
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;
}
}