关于vb.net连接Access数据库的问题,老出错!请各位高手帮忙!
module块的代码:ImportsSystemImportsADODBModuleModule1PublicdbConnAsADODB.Connection'定义一个A...
module块的代码:
Imports System
Imports ADODB
Module Module1
Public dbConn As ADODB.Connection '定义一个ADODB连接
'Public stuRs As ADODB.Recordset '定义Recordset对象
'Public SqlFindStr As String
Public Function Conn2DB() As Boolean
'公共Access数据库连函数
Dim conStr As String
Dim dbName As String
dbName = "D:\Fam_Money_Manger_System1\Database\" & "Money_Manage.accdb" '返回程序所在路径
' Access连接字符串
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbName & ";Jet OLEDB:Database Password=;"
dbConn = New ADODB.Connection '定义一个ADODB连接对象
If dbConn.State <> ADODB.ObjectStateEnum.adStateOpen Then '判断ADODB连接是否打开
dbConn.Open(conStr) '如果不是打开状态 就打开连接
End If
Conn2DB = True
End Function
Public Function ExcSQL(ByVal sql As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Call Conn2DB()
'执行一条SQL查询,得到一个记录集
rst = New ADODB.Recordset
Try
rst.Open(Trim$(sql), dbConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) '得到记录
Catch ex As Exception
MsgBox(ex.Message)
End Try
ExcSQL = rst
'MsgBox(sql)
End Function
Public Function CloseDb() As Boolean
If dbConn.State = ADODB.ObjectStateEnum.adStateOpen Then
dbConn.Close() : dbConn = Nothing
End If
End Function
End Module
Form里面的代码(实现用户登录功能):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
rs1 = ExcSQL("Select * from User where Username=" & "'UserName.Text'" & "")
rs2 = ExcSQL("Select Password from User where Username=" & "'UserName.Text'" & "")
If (rs1.EOF = True And rs1.BOF = True) Then
MsgBox("用户名不存在,请重新输入或注册!")
UserName.Text = ""
UserPWD.Text = ""
Vcode.Text = ""
Call VcCode()
Else
If UserPWD.Text = Str(rs2) Then
If Vcode.Text = Label6.Text Then
FormMain.Show()
Else
MsgBox("验证码输入错误,请重新输入!")
Call VcCode()
Vcode.Text = ""
End If
Else
MsgBox("密码输入错误,请重新输入!")
UserPWD.Text = ""
End If
End If
End Sub
出错地方:Form里 rs1 = ExcSQL("Select * from User where Username=" & "'UserName.Text'" & "")
rs2 = ExcSQL("Select Password from User where Username=" & "'UserName.Text'" & "")这两行,错误信息就是Form里面有错
解决了再给分!
其实导致Form语句出错的原因是 rst.Open(Trim$(sql), dbConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) '得到记录这条语句,这里面我加了处理异常语句,所以没显示在这,显示If (rs1.EOF = True And rs1.BOF = True) Then这条错误,错误信息是对象处于关闭,不能操作。 展开
Imports System
Imports ADODB
Module Module1
Public dbConn As ADODB.Connection '定义一个ADODB连接
'Public stuRs As ADODB.Recordset '定义Recordset对象
'Public SqlFindStr As String
Public Function Conn2DB() As Boolean
'公共Access数据库连函数
Dim conStr As String
Dim dbName As String
dbName = "D:\Fam_Money_Manger_System1\Database\" & "Money_Manage.accdb" '返回程序所在路径
' Access连接字符串
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbName & ";Jet OLEDB:Database Password=;"
dbConn = New ADODB.Connection '定义一个ADODB连接对象
If dbConn.State <> ADODB.ObjectStateEnum.adStateOpen Then '判断ADODB连接是否打开
dbConn.Open(conStr) '如果不是打开状态 就打开连接
End If
Conn2DB = True
End Function
Public Function ExcSQL(ByVal sql As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Call Conn2DB()
'执行一条SQL查询,得到一个记录集
rst = New ADODB.Recordset
Try
rst.Open(Trim$(sql), dbConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) '得到记录
Catch ex As Exception
MsgBox(ex.Message)
End Try
ExcSQL = rst
'MsgBox(sql)
End Function
Public Function CloseDb() As Boolean
If dbConn.State = ADODB.ObjectStateEnum.adStateOpen Then
dbConn.Close() : dbConn = Nothing
End If
End Function
End Module
Form里面的代码(实现用户登录功能):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
rs1 = ExcSQL("Select * from User where Username=" & "'UserName.Text'" & "")
rs2 = ExcSQL("Select Password from User where Username=" & "'UserName.Text'" & "")
If (rs1.EOF = True And rs1.BOF = True) Then
MsgBox("用户名不存在,请重新输入或注册!")
UserName.Text = ""
UserPWD.Text = ""
Vcode.Text = ""
Call VcCode()
Else
If UserPWD.Text = Str(rs2) Then
If Vcode.Text = Label6.Text Then
FormMain.Show()
Else
MsgBox("验证码输入错误,请重新输入!")
Call VcCode()
Vcode.Text = ""
End If
Else
MsgBox("密码输入错误,请重新输入!")
UserPWD.Text = ""
End If
End If
End Sub
出错地方:Form里 rs1 = ExcSQL("Select * from User where Username=" & "'UserName.Text'" & "")
rs2 = ExcSQL("Select Password from User where Username=" & "'UserName.Text'" & "")这两行,错误信息就是Form里面有错
解决了再给分!
其实导致Form语句出错的原因是 rst.Open(Trim$(sql), dbConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) '得到记录这条语句,这里面我加了处理异常语句,所以没显示在这,显示If (rs1.EOF = True And rs1.BOF = True) Then这条错误,错误信息是对象处于关闭,不能操作。 展开
3个回答
展开全部
"Select * from User where Username=" & "'UserName.Text'" & ""
改成
"Select * from User where Username='" & UserName.Text & "'" 试试
UserName.Text是一个变量,本来就是字符串,外面不能用双引号,否则变成常量了。单引号也要分开到两边,不能跟UserName.Text放一起。
下面那句也要改成"Select Password from User where Username='" & UserName.Text & "'"
改成
"Select * from User where Username='" & UserName.Text & "'" 试试
UserName.Text是一个变量,本来就是字符串,外面不能用双引号,否则变成常量了。单引号也要分开到两边,不能跟UserName.Text放一起。
下面那句也要改成"Select Password from User where Username='" & UserName.Text & "'"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果DataGrid绑定数据源为RecordSet记录集的话,需要在改变数据库内容后,
先运行Rs.update,再DataGrid.refresh
先运行Rs.update,再DataGrid.refresh
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把*换成username
更多追问追答
追问
应该不是这个吧!那下一条语句不也有错!
追答
VB的连接字符是用 & 吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询