求助:VB.NET登录界面与数据库的连接问题。
这是我在登录界面的确定下写的一段代码,目的是和数据库连接,数据库名称是NEW
其中adUseClient,adOpenStatic,adLockReadOnly三项是名称未声明
rs.fields是类型“ADODB.Fields”的值无法转换为 "Boolean "。
[0]是需要标示符。
本人纯属菜鸟,希望高手能解释的通俗点 万分感谢!!!!!!!
Private Sub _Command1_0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Command1_0.Click
Dim strSQL As String
Dim cn As New adodb.connection
Dim rs As New adodb.recordset
cn.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa; "
cn.open()
rs.CursorLocation = adUseClient
strSQL = "select 密码 from 表 where 登录名= ' " & txtUserID.Text & " ' "
rs.Open(strSQL, cn, adOpenStatic, adLockReadOnly)
If rs.recordcount = 0 Then
MsgBox( "用户名不正确 ")
Else
if rs.fields[0].value = txtPwd.text then
MsgBox( "登录成功 ")
Else
MsgBox( "密码错误 ")
End If
End If
End Sub
[解决办法]
这样看不出什么问题,你可以单条语句执行看看,哪 行出了问题
[解决办法]
Imports ADODB
Imports ADODB.CursorLocationEnum '这个为了声明这个CursorLocation 的
Imports ADODB.CursorTypeEnum '为了声明adOpenStatic
Imports ADODB.LockTypeEnum 'adLockReadOnly
'-----------------
'以上是声明 ADODB的命名空间
'-----------------
Private Sub _Command1_0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Command1_0.Click
Dim strSQL As String
Dim cn As New adodb.connection
Dim rs As New adodb.recordset
cn.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa; "
cn.open()
rs.CursorLocation = adUseClient
strSQL = "select 密码 from 表 where 登录名= ' " & txtUserID.Text & " ' "
rs.Open(strSQL, cn, adOpenStatic, adLockReadOnly)
If rs.recordcount = 0 Then
MsgBox( "用户名不正确 ")
Else
If rs.Fields(0).Value = txtPwd.text Then '这里你有个错误,不用中括号[],使用()
MsgBox( "登录成功 ")
Else
MsgBox( "密码错误 ")
End If
End If
End Sub