使用asp.net制作注册表单
本文关键字:注册 注册表 表单 asp net 使用 | 更新日期: 2023-09-27 18:17:23
我希望每当新用户注册时。c#代码应该检查数据库表中的值是否存在同名的用户。我使用sql-server存储数据,并使用过程插入数据。我在表中使用一个唯一的约束,如果用户具有相同的名字存在,它应该在屏幕上给出一个错误消息。
过程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>
实际上可以直接从过程中设置条件,即只在不存在时插入,如下所示:
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();
}