更新Asp.net中的数据库(Sql)
我尝试更改所有内容并使用新命令来更正此代码,但什么也没发生。我总是收到错误或者数据库根本没有改变。
在我的数据库中,我有表名(登录)并有(密码),(学生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>
<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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用了错误类型的参数语法:
这适用于 ODBC 连接(如果我没记错的话) - 但对于 SQL Server Express,您需要使用命名参数:
您可能还想参数化
学生ID
...You're using the wrong kind of parameter syntax:
That would work for an ODBC connection (if I'm not mistaken) - but for SQL Server Express, you need to use a named parameter:
You might also want to parametrize the
StudentID
...