更新Asp.net中的数据库(Sql)

发布于 2024-11-07 09:10:25 字数 3364 浏览 2 评论 0原文

我尝试更改所有内容并使用新命令来更正此代码,但什么也没发生。我总是收到错误或者数据库根本没有改变。

在我的数据库中,我有表名(登录)并有(密码),(学生ID) 这是我的代码:

Imports System.Data
Imports System.Data.SqlClient


Partial Class Default2
    Inherits System.Web.UI.Page




    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim field1 = CType(Session.Item("UserAuthentication"), String)
        Dim connectionstring As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EEAS.mdf;Integrated Security=True;User Instance=True"

        If Page.IsValid Then
            Dim MyConnection As New SqlConnection(connectionstring)
            Dim MyCommand As New SqlCommand
            MyCommand.Connection = MyConnection
            MyCommand.CommandType = Data.CommandType.Text
            MyCommand.CommandText = "UPDATE LogIn SET [Password] = ? WHERE StudentID = 123456"
            MyCommand.Parameters.AddWithValue("Password", TextBox2.Text)
            ' MyCommand.Parameters.AddWithValue("Username", TextBox1.Text)
            'MyCommand.Parameters.AddWithValue("Password2", TextBox3.Text)
            Try
                MyConnection.Open()
                Dim RecordsAffected As Int32 = MyCommand.ExecuteNonQuery
                If RecordsAffected = 1 Then
                    Label4.CssClass = "Success"
                    Label4.Text = "Password is changed"
                Else
                    Label4.CssClass = "Error"
                    Label4.Text = "Password is not changed"
                End If
            Catch ex As Exception
                Label4.CssClass = "Error"
                Label4.Text = "Database Error"
            Finally
                MyConnection.Close()
            End Try
        End If



    End Sub
End Class

这是我在 asp.net 中的代码

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        <br />
    </p>
    <p>
        &nbsp; <span class="style2">To change your password, please fill the form below:</span></p>
    <p class="style3"> Change Your Password</p>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Password:"></asp:Label>
        &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </p>
    <p>
                <asp:Label ID="Label2" runat="server" Text="New Password:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </p>
    <p>

        <asp:Label ID="Label3" runat="server" Text="Confirm New Password:"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </p>
    <p>
        <asp:Label ID="Label4" runat="server" style="color: #FF0000" Text="The Confirm New Password must match the New Password entry."></asp:Label>
    </p>
    <p>
        <asp:Button ID="Button1" 
            runat="server" Text="Change Password" />
&nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Cancel" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
    </p>
    <p>
   </asp:Content>

I try to change everythings and use new command to correct this code but nothing happened. I always get an error or the database dosen't change at all.

In my database I have Table name ( LogIn) and have (Password),(StudentID)
This is my code:

Imports System.Data
Imports System.Data.SqlClient


Partial Class Default2
    Inherits System.Web.UI.Page




    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim field1 = CType(Session.Item("UserAuthentication"), String)
        Dim connectionstring As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EEAS.mdf;Integrated Security=True;User Instance=True"

        If Page.IsValid Then
            Dim MyConnection As New SqlConnection(connectionstring)
            Dim MyCommand As New SqlCommand
            MyCommand.Connection = MyConnection
            MyCommand.CommandType = Data.CommandType.Text
            MyCommand.CommandText = "UPDATE LogIn SET [Password] = ? WHERE StudentID = 123456"
            MyCommand.Parameters.AddWithValue("Password", TextBox2.Text)
            ' MyCommand.Parameters.AddWithValue("Username", TextBox1.Text)
            'MyCommand.Parameters.AddWithValue("Password2", TextBox3.Text)
            Try
                MyConnection.Open()
                Dim RecordsAffected As Int32 = MyCommand.ExecuteNonQuery
                If RecordsAffected = 1 Then
                    Label4.CssClass = "Success"
                    Label4.Text = "Password is changed"
                Else
                    Label4.CssClass = "Error"
                    Label4.Text = "Password is not changed"
                End If
            Catch ex As Exception
                Label4.CssClass = "Error"
                Label4.Text = "Database Error"
            Finally
                MyConnection.Close()
            End Try
        End If



    End Sub
End Class

This is my code in asp.net

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        <br />
    </p>
    <p>
          <span class="style2">To change your password, please fill the form below:</span></p>
    <p class="style3"> Change Your Password</p>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Password:"></asp:Label>
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </p>
    <p>
                <asp:Label ID="Label2" runat="server" Text="New Password:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </p>
    <p>

        <asp:Label ID="Label3" runat="server" Text="Confirm New Password:"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </p>
    <p>
        <asp:Label ID="Label4" runat="server" style="color: #FF0000" Text="The Confirm New Password must match the New Password entry."></asp:Label>
    </p>
    <p>
        <asp:Button ID="Button1" 
            runat="server" Text="Change Password" />
  
        <asp:Button ID="Button2" runat="server" Text="Cancel" />
    </p>
    <p>
         </p>
    <p>
         </p>
    <p>
    </p>
    <p>
   </asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-11-14 09:10:25

您使用了错误类型的参数语法:

MyCommand.CommandText = "UPDATE LogIn SET [Password] = ? WHERE StudentID = 123456"

这适用于 ODBC 连接(如果我没记错的话) - 但对于 SQL Server Express,您需要使用命名参数:

 MyCommand.CommandText = "UPDATE dbo.LogIn SET [Password] = @Password WHERE StudentID = 123456"
 MyCommand.Parameters.AddWithValue("@Password", TextBox2.Text)

您可能还想参数化 学生ID ...

You're using the wrong kind of parameter syntax:

MyCommand.CommandText = "UPDATE LogIn SET [Password] = ? WHERE StudentID = 123456"

That would work for an ODBC connection (if I'm not mistaken) - but for SQL Server Express, you need to use a named parameter:

 MyCommand.CommandText = "UPDATE dbo.LogIn SET [Password] = @Password WHERE StudentID = 123456"
 MyCommand.Parameters.AddWithValue("@Password", TextBox2.Text)

You might also want to parametrize the StudentID ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文