我想知道下面的代码有什么问题。我正在创建一个用户可以更改密码的表单,但每当我尝试运行系统时总是会出错。任何帮助将不胜感激!谢谢。
Public Class Form7
Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Using dbCommand As New System.Data.OleDb.OleDbCommand("select count ([Password]) from students where [Password] like ?", cnString)
dbCommand.Parameters.AddWithValue("@password", textbox1.Text)
Dim result As Integer = DirectCast(dbCommand.ExecuteScalar(), Integer)
If result = 1 Then
Dim sqlquery As String = "UPDATE students SET StudFacID = @STUDID, [Password] = @Password" & _
"WHERE ID = StudFacID"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, cnString)
cnString.Open()
cmd.Parameters.AddWithValue("@STUDID", TxtID.Text)
cmd.Parameters.AddWithValue("@Password", TextBox2.Text)
End Using
End Using
End If
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
结束班
答案 0 :(得分:1)
您正在尝试构建OldDbCommand
对象:
New System.Data.OleDb.OleDbCommand("your sql command", cnString)
使用the constructor that takes two arguments。该构造函数需要String
和OleDbConnection
,但您需要为String
和String
提供String
和OldDbConnection
。如错误所示,Using dbConnection As New OleDbConnection(cnString)
Using dbCommand As New OleDbCommand("your sql command", dbConnection)
' the rest of your code
End Using
End Using
无法转换为conn
,因此此尝试失败。
创建an OleDbConnection
object以与命令对象一起使用。也许是这样的:
cmd
(基本上,您应该使用{{1}}和{{1}}对象执行完全相同的操作。)
此外,作为附注,这是非常重要 ...您应 不 将用户密码存储为 纯文字 。用户密码应该进行哈希处理,并且永远不会可以恢复。您目前正在做的事情是对您的用户非常不负责任。