mysql存储过程及c#调用标准版
2007-11-11 13:19:38 来源:WEB开发网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值
MySQL(和PHP搭配之最佳组合)Connection MySQL(和PHP搭配之最佳组合)con;
MySQL(和PHP搭配之最佳组合)DataAdapter MySQL(和PHP搭配之最佳组合)data = new MySQL(和PHP搭配之最佳组合)DataAdapter();
DataSet dataset = new DataSet();
MySQL(和PHP搭配之最佳组合)con = new MySQL(和PHP搭配之最佳组合)Connection("server=localhost; user id=root; password=123; database=yuan_test; pooling=false;charset=gb2312");
MySQL(和PHP搭配之最佳组合)data.SelectCommand = new MySQL(和PHP搭配之最佳组合)Command();
MySQL(和PHP搭配之最佳组合)data.SelectCommand.Connection = MySQL(和PHP搭配之最佳组合)con;
MySQL(和PHP搭配之最佳组合)data.SelectCommand.CommandText = "yuan_user_cc";
MySQL(和PHP搭配之最佳组合)data.SelectCommand.CommandType = CommandType.StoredProcedure;
//设置参数,添加到数据库
MySQL(和PHP搭配之最佳组合)Parameter name_parameter = new MySQL(和PHP搭配之最佳组合)Parameter("?xname", MySQL(和PHP搭配之最佳组合)DbType.VarChar,100);//MySQL(和PHP搭配之最佳组合)的存储过程参数是以?打头的!!!!
name_parameter.Value = name;
MySQL(和PHP搭配之最佳组合)data.SelectCommand.Parameters.Add(name_parameter);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MySQL(和PHP搭配之最佳组合)Parameter email_parameter = new MySQL(和PHP搭配之最佳组合)Parameter("?xemail", MySQL(和PHP搭配之最佳组合)DbType.VarChar, 100);//MySQL(和PHP搭配之最佳组合)的存储过程参数是以?打头的!!!!
email_parameter.Value = email;
MySQL(和PHP搭配之最佳组合)data.SelectCommand.Parameters.Add(email_parameter);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MySQL(和PHP搭配之最佳组合)Parameter password_parameter = new MySQL(和PHP搭配之最佳组合)Parameter("?xpassword", MySQL(和PHP搭配之最佳组合)DbType.VarChar, 100);//MySQL(和PHP搭配之最佳组合)的存储过程参数是以?打头的!!!!
password_parameter.Value = password;
MySQL(和PHP搭配之最佳组合)data.SelectCommand.Parameters.Add(password_parameter);
//++++++++++++++++++++++++++返回值++++++++++++++++++++++++++++++++++++++++++
MySQL(和PHP搭配之最佳组合)Parameter id_parameter = new MySQL(和PHP搭配之最佳组合)Parameter("?xid", MySQL(和PHP搭配之最佳组合)DbType.Int16,15);//MySQL(和PHP搭配之最佳组合)的存储过程参数是以?打头的!!!!
id_parameter.Direction=ParameterDirection.Output;
MySQL(和PHP搭配之最佳组合)data.SelectCommand.Parameters.Add(id_parameter);
//+++++++++++++++++++++++++打开数据库,插入数据,返回id值+++++++++++++++++++
try
{
MySQL(和PHP搭配之最佳组合)data.Fill(dataset, "yuan_user_cc");
id = (Int16)id_parameter.Value;
return id.ToString();
}
catch (MySQL(和PHP搭配之最佳组合)Exception ex)
{
MessageBox.Show(ex.Message);
return String.Empty;
}
}
更多精彩
赞助商链接