c# - SQL Server 数据库不会更新来自网站的信息

标签 c# asp.net sql-server

我刚刚开始使用 C# 进行 Web 开发,所以我还是个新手。我正在尝试做一个基本的“注册新用户代码”我正在引用我保存在 SQL Server 中的存储过程,一切似乎都很好。但是由于我对网络开发还很陌生,所以我不确定我是否遗漏了什么。问题是数据库不会更新。我运行应用程序并输入信息,单击按钮后,我刷新了数据库,但它没有插入。很确定存储过程工作正常......查询测试有效。

C#:

namespace WebApplication2.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void OnClick(object sender, EventArgs e)
        {
            String connstr = ConfigurationManager.ConnectionStrings["PracticeConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("InsertUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName.Text;
            cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email.Text;
            cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password.Text;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                Response.Redirect("About.aspx");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

Asp.Net:

<%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Register.aspx.cs" Inherits="WebApplication2.Account.Register" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:PracticeConnectionString %>" 
    SelectCommand="SELECT * FROM [User]" InsertCommand="InsertUser" 
        InsertCommandType="StoredProcedure">
        <InsertParameters>
            <asp:Parameter Name="UserName" Type="String" />
            <asp:Parameter Name="Password" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

        <table style="font-size:100%;width:388px;">
            <tr>
                <td align="center" colspan="2">
                    Sign Up for Your New Account</td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                        ControlToValidate="UserName" ErrorMessage="User Name is required." 
                        ToolTip="User Name is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                        ControlToValidate="Password" ErrorMessage="Password is required." 
                        ToolTip="Password is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="ConfirmPasswordLabel" runat="server" 
                        AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                        ControlToValidate="ConfirmPassword" 
                        ErrorMessage="Confirm Password is required." 
                        ToolTip="Confirm Password is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                        ControlToValidate="Email" ErrorMessage="E-mail is required." 
                        ToolTip="E-mail is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" 
                        ControlToValidate="Question" ErrorMessage="Security question is required." 
                        ToolTip="Security question is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" 
                        ControlToValidate="Answer" ErrorMessage="Security answer is required." 
                        ToolTip="Security answer is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:CompareValidator ID="PasswordCompare" runat="server" 
                        ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                        Display="Dynamic" 
                        ErrorMessage="The Password and Confirmation Password must match." 
                        ValidationGroup="RegisterUser"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="color:Red;">
                    <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                </td>
            </tr>
        </table>

    <asp:Button ID="Button1" runat="server" onclick="OnClick" Text="Create User" />
</asp:Content>

最佳答案

这是添加参数的更好方法:cmd.Parameters.Add(new SqlParameter("@UserName", UserName.Text));

关于c# - SQL Server 数据库不会更新来自网站的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17239505/

相关文章:

c# - App.config 相对路径

c# - 找不到 IronPython sys._getframe

c# - 调试 visual studio 代码时 launch.json 中出现 'program' 错误

c# - 无法在 Windows Phone 7 中将字符串解析为 DateTime

sql - 在存储过程中选择多个 XML 元素

c# - 数据未从 View (ajax)发送到 Controller (c#)

asp.net - 什么是 ASPXAUTH cookie?

尽管有 IRequireSessionState/IReadOnlySessionState 处理程序,但 ASP.NET session 为 NULL?

sql-server - 改变列数据类型

sql - 优化 Soundex 查询以查找相似的名称