2016-11-10 116 views
-2

我希望每當新用戶註冊時。 C#代碼應檢查數據庫表中的值是否存在具有相同名稱的用戶。我正在使用sql-server來存儲數據和插入數據的過程。我在表中使用唯一的約束,如果存在同名的用戶,它應該在屏幕上給出錯誤信息。使用asp.net註冊表單

程序

CREATE PROCEDURE [dbo].[SigningIn] 
@UserName nvarchar(90), 
@Password nvarchar(90) 
AS 

insert into Signin(UserName,UserPassword) values 
        (@UserName,@Password) 
RETURN 

C#代碼

if(user name doesn't exists) 
     var command = new SqlCommand("SigningIn", connection) 
     { 
      CommandType = CommandType.StoredProcedure 
     }; 
     command.Parameters.AddWithValue("@UserName", UserNameBox.Text); 
     command.Parameters.AddWithValue("@Password", PasswordBox.Text); 
     command.ExecuteNonQuery(); 
     connection.Close(); 
    } 
    else 
    { 
     UserExists.Text = "User Already Exists"; 
    } 

asp代碼

<asp:Label CssClass="col-sm-6 control-label " ID="UserName" runat="server" Text="User name"></asp:Label> 
    <div class="col-sm-6 "> 
     <asp:TextBox CssClass="form-control" placeholder="Pick a user name" ID="UserNameBox" runat="server"></asp:TextBox> 
     <asp:RequiredFieldValidator CssClass="validators" ID="UserValidator" runat="server" ErrorMessage="Field is Required" ForeColor="#FF3300" ControlToValidate="UserNameBox" SetFocusOnError="True" ClientIDMode="Static" Display="Dynamic" ViewStateMode="Enabled"></asp:RequiredFieldValidator> 
    </div></div></div> 
      <div class="form-horizontal "> 
      <div class="form-group "> 

    <asp:Label CssClass="col-sm-6 control-label " ID="Password" runat="server" Text="Password"></asp:Label> 
    <div class="col-sm-6 "> 
    <asp:TextBox CssClass="form-control" placeholder="Enter password" ID="PasswordBox" runat="server" TextMode="Password" ></asp:TextBox> 
     <asp:RequiredFieldValidator CssClass="validators" ID="passwordValidator" runat="server" ErrorMessage="Field is Required" ForeColor="#FF3300" ControlToValidate="PasswordBox" SetFocusOnError="True" ClientIDMode="Static" Display="Dynamic" ViewStateMode="Enabled">     </asp:RequiredFieldValidator> 

</div></div></div> 
       <div class="form-horizontal "> 
      <div class="form-group "> 


    <asp:Label CssClass="col-sm-6 control-label " ID="ConfirmLabel" runat="server" Text="Confirm Password"></asp:Label> 
    <div class="col-sm-6 "> 
     <asp:TextBox CssClass="form-control" placeholder="Confirm Password" TextMode="Password" ID="ConfirmBox" runat="server"></asp:TextBox> <asp:Label ID="UserExists" CssClass="validators" runat="server" Text="" ></asp:Label> 
     <asp:RequiredFieldValidator CssClass="validators" ID="confirmValidator" runat="server" ErrorMessage="Field is required" ControlToValidate="ConfirmBox" SetFocusOnError="True" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator> 
     <asp:CompareValidator ID="comparePassword" CssClass="validators" runat="server" ErrorMessage="Password doesn't match" SetFocusOnError="True" ControlToCompare="PasswordBox" ControlToValidate="ConfirmBox" ForeColor="#FF3300"></asp:CompareValidator> 
+0

等都不是代碼寫作服務[查看如何提出一個問題,這個指南](http://stackoverflow.com/help/how-to -問)。如果有什麼特別的東西卡住了,那就問問這個問題。 – Slappywag

+0

http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ –

回答

0

實際上,你可以直接從你的程序,這是插入,只有當它不」設定的條件t存在,如下所示:

CREATE PROCEDURE [dbo].[SigningIn] 
@UserName nvarchar(90), 
@Password nvarchar(90) 
AS 
BEGIN 
IF NOT EXISTS(SELECT * FROM Signin WHERE UserName = @UserName) 
BEGIN 
INSERT INTO Signin(UserName,UserPassword) VALUES(@UserName,@Password); 
END 
END 

還是繼續用C#代碼:

using(SqlConnection connection = new SqlConnection(connString)){ 
connection.Open(); 
bool userNameExists = false; 

var command = 
new SqlCommand("SELECT COUNT(*) FROM Signin WHERE UserName = @UserName", connection); 
command.Parameters.AddWithValue("@UserName",UserNameBox.Text.Trim(); 
userNameExists = (int)command.ExecuteScalar() > 0; 

//if exists 
if(userNameExists) { 
UserExists.Text = "User Already Exists"; 
}else{ 
command = new SqlCommand("SigningIn", connection); 
command.CommandType = CommandType.StoredProcedure; 
command.Parameters.AddWithValue("@UserName", UserNameBox.Text); 
command.Parameters.AddWithValue("@Password", PasswordBox.Text); 
command.ExecuteNonQuery(); 
connection.Close(); 
} 
+0

其實我在查詢中使用文本框的直接值做一個檢查操作。像userName.text – Learner

+0

什麼是** executeScalar **方法。我只知道ExecuteNonQuery和ExecuteReader – Learner

+0

執行標量值意味着只獲取一個或一組值。如果您不需要獲取整行的所有值 – zhack