2011-05-15 119 views
0

我尝试改变一切,并使用新命令来更正此代码,但没有发生任何事情。我总是得到一个错误或数据库根本不会改变。更新Asp.net中的数据库(Sql)

在我的数据库我有表名(登录)和具有(密码),(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> 
+2

你为什么不告诉我们你得到了什么错误? – blowdart 2011-05-15 19:48:14

回答

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 ...

+0

+1不知道这是否是整个问题,但至少是错误的一部分。而且,是的,位置参数可以与ODBC一起使用。 – 2011-05-15 21:30:39