我在VB.NET中有一个函数,用于在启动主应用程序之前向MySQL数据库验证用户身份。这是函数的代码:
Public Function authConnect() As Boolean
Dim dbserver As String
Dim dbuser As String
Dim dbpass As String
dbserver = My.Settings.dbserver.ToString
dbuser = My.Settings.dbuser.ToString
dbpass = My.Settings.dbpass.ToString
conn = New MySqlConnection
myConnString = "server=" & dbserver & ";" & "user id=" & dbuser & ";" & "password=" & dbpass & ";" & "database=rtadmin"
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim myDataReader As New MySqlDataReader
Dim query As String
myCommand.Parameters.Add(New MySqlParameter("?Username", login_usr_txt.Text))
myCommand.Parameters.Add(New MySqlParameter("?Password", login_pass_txt.Text))
query = "select * from users where user = ?Username and passwd = ?Password"
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = query
myAdapter.SelectCommand = myCommand
myDataReader = myCommand.ExecuteReader
If myDataReader.HasRows() Then
MessageBox.Show("You've been logged in.", "RT Live! Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
End Try
Catch ex As Exception
End Try
End Function
该功能尚未完成,在启动应用程序之前还需要完成其他一些事情,因为我使用MessageBox来显示登录尝试的结果。
我得到的错误如下:
错误1'MySql.Data.MySqlClient.MySqlDataReader.Friend Sub New(cmd As MySql.Data.MySqlClient.MySqlCommand,statement as MySql.Data.MySqlClient.PreparableStatement,behavior As System.Data.CommandBehavior)'无法访问在这种情况下,因为它是'朋友'。 C:\ Users \ Mario \ documents \ visual studio 2010 \ Projects \ Remote Techs Live!\ Remote Techs Live!\ Login.vb 43 13 Remote Techs Live!
有什么想法吗?
答案 0 :(得分:1)
尝试创建一个MySqlDataReader然后扔掉它是没有意义的!
首先,您要尝试创建阅读器:
Dim myDataReader As New MySqlDataReader
然后,当你这样做时,你会尝试扔掉它:
myDataReader = myCommand.ExecuteReader
只需从初始声明中删除New即可。我怀疑MySqlDataReader的构造函数不能公开访问。
答案 1 :(得分:0)
您可以尝试.AddWithValue而不是.Add。
例如:
cmd.Parameters.AddWithValue("?Username", login_usr_txt.Text)