ASP.NET中如何调用存储过程

前天在此转了几个关于存储过程的贴子,都会有错误,自己再做了一遍,把代码发出来给大家谈论一下!

存储过程

CREATE PROCEDURE add_user 
   @user_ID 
varchar(11), 
   @user_Name 
varchar(15), 
   @user_Password 
varchar(50), 
   @true_Name 
varchar(10), 
   @Sex 
varchar(4), 
   @Borth 
datetime
   @Email 
varchar(50), 
   @fixPhone 
varchar(15), 
   @Address 
varchar(50), 
   @mobilePhone 
varchar(12), 
   @postalcode 
varchar(6), 
   @problem 
varchar(50), 
   @answer 
varchar(16), 
   @joinTime 
datetime
   @pr_ID 
int 
AS 
   
DECLARE @Count int 
   
-- 查找是否有相同名称的记录 
   SELECT @Count = Count([user_ID]FROM userInfo 
   
WHERE [user_Name] = @user_Name 

   
IF (@Count = 0
 
INSERT INTO userInfo 
 (
[user_ID],[user_Name],user_Password,true_Name,Sex,Borth,Email,fixPhone,Address,mobilePhone,postalcode,problem,answer,joinTime,pr_ID) 
  
values 
 (@user_ID,@user_Name, @user_Password, @true_Name, @Sex, @Borth, @Email, @fixPhone, @Address, 
  @mobilePhone, @postalcode, @problem, @answer, @joinTime, @pr_ID) 
 
RETURN 1 
GO 


中间层 
 

  /// <summary> 
  
///  添加用户 
  
/// </summary> 
  
/// <param name="_strName">用户名</param> 
  
/// <param name="_strPwd">密码</param> 
  
/// <returns>返回int,1为正确,0为己有该用户,-1为不异常错误</returns> 

  public int add_user(string _strName,string _strPwd,string _strTrueName,string _strSex,string _strBorth,string _strEmail,string _strfixPhone,string _strAddress,string _strMobilePhone,string _strPostalcode,string _strProblem,string _strAnswer,string _strJointime,int _strPr_ID) 
  

   csFile.DBOperation conn 
= new DBOperation(); 
   SqlCommand cmd_add 
= conn.get_sp("add_user"); 
   cmd_add.Parameters.Add(
"@user_Name",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@user_Password",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@true_Name",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@Sex",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@Borth",SqlDbType.DateTime); 
   cmd_add.Parameters.Add(
"@Email",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@fixPhone",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@Address",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@mobilePhone",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@postalcode",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@problem",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@answer",SqlDbType.VarChar); 
   cmd_add.Parameters.Add(
"@joinTime",SqlDbType.DateTime); 
   cmd_add.Parameters.Add(
"@pr_ID",SqlDbType.Int); 
   cmd_add.Parameters.Add(
"@user_ID",SqlDbType.VarChar); 
    
   
//表中最后的ID值 
   string strNewID = ""
   DBOperation connID 
= new DBOperation(); 
   DataSet ds 
= connID.get_dataset("select user_ID from [userInfo]"); 
   
if ( ds.Tables[0].Rows.Count == 0
   

    strNewID 
= "GR000000001"
   }
 
   
else 
   

    
int code=ds.Tables[0].Rows.Count-1
    
string strLastID = ds.Tables[0].Rows[code].ItemArray[0].ToString(); 
    
string strID = strLastID.Substring(2,9); 
    strNewID 
= "GR" + Convert.ToString(Convert.ToInt32(strID) + 1).PadLeft(9,'0'); 
   }
 

   cmd_add.Parameters[
"@user_ID"].Value = strNewID; 
   cmd_add.Parameters[
"@user_Name"].Value = _strName; 
   cmd_add.Parameters[
"@user_Password"].Value = conn.Encrypt(_strPwd); 
   cmd_add.Parameters[
"@true_Name"].Value = _strTrueName; 
   cmd_add.Parameters[
"@Sex"].Value = _strSex; 
   cmd_add.Parameters[
"@Borth"].Value = Convert.ToDateTime(_strBorth); 
   cmd_add.Parameters[
"@Email"].Value = _strEmail; 
   cmd_add.Parameters[
"@fixPhone"].Value = _strfixPhone; 
   cmd_add.Parameters[
"@Address"].Value = _strAddress; 
   cmd_add.Parameters[
"@mobilePhone"].Value = _strMobilePhone; 
   cmd_add.Parameters[
"@postalcode"].Value = _strPostalcode; 
   cmd_add.Parameters[
"@problem"].Value = _strProblem; 
   cmd_add.Parameters[
"@answer"].Value = _strAnswer; 
   cmd_add.Parameters[
"@joinTime"].Value = Convert.ToDateTime(_strJointime); 
   cmd_add.Parameters[
"@pr_ID"].Value = _strPr_ID; 
    
   
//返回用户编号 
   int rowsAffrected = cmd_add.ExecuteNonQuery(); 

   
return rowsAffrected; 
  }
 


实现功能

private void btReg_Click(object sender, System.EventArgs e) 
  

   user user_add 
= new user(); 
   DateTime joinTime 
= DateTime.Now; 
   
int flag = user_add.add_user(this.txtUserName.Text.Trim().ToString(),this.txtPwd.Text.Trim().ToString(),this.txtRealName.Text.Trim().ToString(), 
    
this.rblSex.SelectedValue.ToString(),this.txtBorth.Text.Trim().ToString(),this.txtEmail.Text.Trim().ToString(), 
    
this.txtPhone.Text.Trim().ToString(),this.txtAddress.Text.Trim().ToString(),this.txtMobilePhone.Text.Trim().ToString(), 
    
this.txtPostalcode.Text.Trim().ToString(),this.txtProblem.Text.Trim().ToString(),this.txtAnswer.Text.Trim().ToString(),joinTime.Date.ToShortDateString(),0); 

   
if ( flag == 1 ) 
   

    
this.Response.Write("<script language=javascript>parent.parent.location.href='login.aspx';</script>"); 
   }
 
   
else if ( flag == -1 ) 
   

    
this.Response.Write("该用户己存在!"); 
   }
 
  }
 
posted on 2005-02-04 09:28  蜗牛设计  阅读(328)  评论(0)    收藏  举报