2012-02-29 53 views
0

我的代碼工作正常,它說該行已保存,但是當我查看數據庫時沒有保存行。你能幫我嗎?在ASP.NET中使用存儲過程保存數據庫中的行時遇到問題

我想使用存儲過程保存一行,checkifusernameexist正在工作,但保存功能不保存數據。

這是我的代碼,希望你能理解它。

using System; 
using System.Data; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.ComponentModel; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Collections.Generic; 
using System.IO; 

namespace storedprocedures 
{ 
    public partial class Login : System.Web.UI.Page 
    { 
     public string GetConnectionString() 
     { 
      return System.Configuration.ConfigurationManager.ConnectionStrings["practiceDatabaseConnectionString"].ConnectionString; 
     } 

     private void executeAdd(string username, string password) 
     { 
      SqlConnection conn = new SqlConnection(GetConnectionString()); 
      conn.Open(); 

      SqlCommand cmd = new SqlCommand("usp_CreateUser", conn); 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlParameter p1 = new SqlParameter("userName",userName.Text); 
      SqlParameter p2 = new SqlParameter("password",password.ToString()); 
      cmd.Parameters.Add(p1); 
      cmd.Parameters.Add(p2); 

      SqlDataReader dReader = cmd.ExecuteReader(); 

      if (dReader.HasRows) 
      { 
       dReader.Read();  
       Label3.Text="Record Added!"; 
      } 
      else 
      { 
       Label3.Text="User Error"; 
      } 
     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(GetConnectionString()); 
      con.Open(); 

      SqlCommand cmd = new SqlCommand("usp_CheckIfUserNameExist",con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlParameter p1 = new SqlParameter("userName", userName.Text); 
      cmd.Parameters.Add(p1); 
      SqlDataReader dReader = cmd.ExecuteReader(); 

      if (dReader.HasRows) 
      { 
       dReader.Read(); 
       this.Label3.ForeColor = System.Drawing.Color.Red; 
       this.Label3.Text = "Username already exist!"; 
      } 
      else 
      { 
       this.Label3.Text = "congrats! nanalo ka na!"; 
       this.userName.Text = ""; 
       this.password.Text = ""; 
      } 
     } 
    } 
} 
+0

您需要**顯示我們**您的連接字符串! – 2012-02-29 06:17:14

回答

3

使用cmd.ExecuteNonQuery()增加記錄。

+2

cmd.ExecuteNonQuery()的返回值是一個整數,您可以檢查查詢修改了多少行。 – Developer 2012-02-29 06:22:05

+0

是的,它返回修改了多少行。 – Saurabh 2012-02-29 06:23:34

相關問題