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

时间:2011-05-15 19:33:39

标签: asp.net asp.net-membership

我尝试更改所有内容并使用新命令更正此代码但没有任何反应。我总是收到错误,或者数据库根本没有变化。

在我的数据库中,我有表名(LogIn)并且有(密码),(StudentID) 这是我的代码:

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>

1 个答案:

答案 0 :(得分:1)

您使用的是错误的参数语法:

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)

您可能还想参数化StudentID ...