切换案例默认情况下是case-1我不明白这段代码请帮助我 [英] Switch case by default it is take case-1 I dont understood this code pls help me

查看:58
本文介绍了切换案例默认情况下是case-1我不明白这段代码请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 CREATE PROCEDURE [dbo]。[Insert_User] 
@Username NVARCHAR( 20 ),
@密码NVARCHAR( 20 ),
@Email NVARCHAR( 30
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT UserId FROM Users WHERE Username = @Username)
BEGIN
SELECT -1 - 用户名存在。
END
ELSE IF EXISTS(SELECT UserId FROM Users WHERE Email = @Email)
BEGIN
SELECT -2 - 电子邮件存在。
结束
ELSE
BEGIN
INSERT INTO [用户]
([用户名]
,[密码]
,[电子邮件]
,[CreatedDate])
VALUES
(@Username
,@ Password
,@ Email
,GETDATE())

SELECT SCOPE_IDENTITY() - UserId
END
END













受保护 void RegisterUser( object sender,EventArgs e)
{
int userId = 0 ;
string constr = ConfigurationManager.ConnectionStrings [ 构造]的ConnectionString。
使用(SqlConnection con = new SqlConnection(constr))
{
使用(SqlCommand cmd = new SqlCommand( Insert_User))
{
使用(SqlDataAdapter sda = < span class =code-keyword> new
SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Username,txtUsername.Text.Trim() );
cmd.Parameters.AddWithValue( @ Password,txtPassword.Text.Trim() );
cmd.Parameters.AddWithValue( @ Email,txtEmail.Text.Trim() );
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
string message = string .Empty;
switch (userId)
{
case -1:
message = 用户名已存在。\\ n请选择其他用户名。;
break ;
case -2:
message = 提供的电子邮件地址已被使用。;
break ;
默认
message = 注册成功。\\\\
User Id:
+ userId.ToString();
break ;
}
ClientScript.RegisterStartupScript(GetType(), alert,< span class =code-string> alert(' + message + '); true );
}
}


设计代码


< table border = 0 cellpadding = 0 cellspacing = 0 >
< tr>
< th colspan = 3 >
注册
< / th >
< / tr < span class =code-keyword>>
< tr>
< td>
用户名
< / td >
< td>
< asp:TextBox ID = txtUsername runat = server />
< / td >
< td>
< asp:RequiredFieldValidator ErrorMessage = 必需 ForeColor = Red ControlToValidate = txtUsername
runat = server />
< / td >
< / tr >
< tr>
< td>
密码
< / td >
< td>
< asp:TextBox ID = txtPassword runat = server TextMode = 密码 />
< / td >
< td>
< asp:RequiredFieldValidator ErrorMessage = 必需 ForeColor = Red ControlToValidate = txtPassword
runat = server />
< / td >
< / tr >
< tr>
< td>
确认密码
< / td >
< td>
< asp:TextBox ID = txtConfirmPassword runat = server TextMode = 密码 />
< / td >
< td>
< asp:CompareValidator ErrorMessage = 密码不匹配。 ForeColor = Red ControlToCompare = txtPassword
ControlToValidate = txtConfirmPassword runat = server />
< / td >
< / tr >
< tr>
< td>
电子邮件
< / td >
< td>
< asp:TextBox ID = txtEmail runat = server />
< / td >
< td>
< asp:RequiredFieldValidator ErrorMessage = 必需显示= 动态 ForeColor = 红色
ControlToValidate = txtEmail runat = server />
< asp:RegularExpressionValidator runat = server Display = 动态 ValidationExpression = \w +([ - +'] \w +)* @ \w +([ - ] \w +)* \.\w +([ - ] \w +)*
ControlToValidate = txtEmail ForeColor = 红色 ErrorMessage = 无效电子邮件地址。 />
< / td >
< / tr >
< tr>
< td>
< / td >
< td>
< asp:Button Text = 提交 runat = server OnClick = RegisterUser />
< / td >
< td>
< / td >
< / tr >
< / table >

我尝试过:

< span class =code-keyword>我们执行的代码然后我们在cmd.executescalr上取一个 break 点,userid取结果 value 然后我们继续 switch case by 默认 取case-1我不明白 this code pls help me

解决案
通过看你的数据开始。使用调试器,并找出您传递给SQL的确切内容。然后使用SSMS查看数据库内容并找出Users表的Username列中的确切内容。

如果查询返回-1,则表明该名称已经存在,因此,您的数据是第一个开始的地方。

然后检查您的连接字符串 - 您是否在正确的服务器上查找正确的数据库?

如果您不能看到任何明显的东西,然后直接使用SSMS执行SP,看看会发生什么。

修改你的SP,找到匹配时返回(测试)用户信息,然后再次在SSMS中运行它。你得到了什么?

我们无法为你做任何事 - 我们无法访问你的数据库。



但不要这样做:以直接文本存储密码是令人厌恶的!

请参阅:密码存储:如何操作。 [ ^ ]

这里:代码犯罪:基于文本的密码 [ ^ ]


CREATE PROCEDURE [dbo].[Insert_User]
      @Username NVARCHAR(20),
      @Password NVARCHAR(20),
      @Email NVARCHAR(30)
AS
BEGIN
      SET NOCOUNT ON;
      IF EXISTS(SELECT UserId FROM Users WHERE Username = @Username)
      BEGIN
            SELECT -1 -- Username exists.
      END
      ELSE IF EXISTS(SELECT UserId FROM Users WHERE Email = @Email)
      BEGIN
            SELECT -2 -- Email exists.
      END
      ELSE
      BEGIN
            INSERT INTO [Users]
                     ([Username]
                     ,[Password]
                     ,[Email]
                     ,[CreatedDate])
            VALUES
                     (@Username
                     ,@Password
                     ,@Email
                     ,GETDATE())
            
            SELECT SCOPE_IDENTITY() -- UserId                  
     END
END













protected void RegisterUser(object sender, EventArgs e)
{
    int userId = 0;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("Insert_User"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                cmd.Connection = con;
                con.Open();
                userId = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
        }
        string message = string.Empty;
        switch (userId)
        {
            case -1:
                message = "Username already exists.\\nPlease choose a different username.";
                break;
            case -2:
                message = "Supplied email address has already been used.";
                break;
            default:
                message = "Registration successful.\\nUser Id: " + userId.ToString();
                break;
        }
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
    }
}


Desiging Code


<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <th colspan="3">
            Registration
        </th>
    </tr>
    <tr>
        <td>
            Username
        </td>
        <td>
            <asp:TextBox ID="txtUsername" runat="server" />
        </td>
        <td>
            <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtUsername"
                runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Password
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
        </td>
        <td>
            <asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtPassword"
                runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Confirm Password
        </td>
        <td>
            <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" />
        </td>
        <td>
            <asp:CompareValidator ErrorMessage="Passwords do not match." ForeColor="Red" ControlToCompare="txtPassword"
                ControlToValidate="txtConfirmPassword" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Email
        </td>
        <td>
            <asp:TextBox ID="txtEmail" runat="server" />
        </td>
        <td>
            <asp:RequiredFieldValidator ErrorMessage="Required" Display="Dynamic" ForeColor="Red"
                ControlToValidate="txtEmail" runat="server" />
            <asp:RegularExpressionValidator runat="server" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Invalid email address." />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text="Submit" runat="server" OnClick="RegisterUser" />
        </td>
        <td>
        </td>
    </tr>
</table>

What I have tried:

in this code we execute then  we take a break point on cmd.executescalr, and userid take the result value and after that we  are going on switch case by default it is take case-1 i dont understood this code pls help me

解决方案

Start by looking at your data. Use the debugger, and find out exactly what you are passing to SQL. Then use SSMS to look at the database content and find out exactly what is in the Username column of the Users table.
If your query is returning -1, then it looks as if the name already exists, so your data is the first place to start.
Then check your connection string - are you looking at the right database, on the right server?
If you can't see anything obvious, then use SSMS directly to execute the SP and see what happens.
Modify your SP to return (for testing) the user information when it finds a match, and run it in SSMS again. What do you get?
We can't do any of this for you - we don't have access to your DB.

But don't do that: storing passwords in straight text is an abomination!
See here: Password Storage: How to do it.[^]
And here: Code crime: text based passwords[^]


这篇关于切换案例默认情况下是case-1我不明白这段代码请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆