我该如何解决这个错误? "无法识别的数据库格式“ 。 [英] How Can I Fix This Error ? " Unrecognized Database Format" .
问题描述
事实:我正在尝试将VB6登录表单连接到MS访问数据库。条件是,当我登录vb登录表单时,它应该检查用户名和密码是否正确。如果正确的另一种形式应该打开哪个名称是mainmenu。
这里的错误是无法识别的数据库格式。我不知道为什么它显示这个错误,因为我把它正确的文件和文件扩展名。请帮助我如何在这种情况下工作。你有任何其他想法连接和检查密码和用户名。
' CHECKING用户名和密码
公共 功能 validuser(用户名作为 字符串,密码作为 字符串)
Dim db As 字符串
Dim Cmd 作为 字符串
Dim sql As 字符串
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset
db = App.Path& \ nfl.accdb
Cmd = Provider = Microsoft.Jet.OLEDB.4.0;& _
数据源=& db&
Set Cn = 新 ADODB.Connection
使用 Cn
.ConnectionString = Cmd
。打开
结束 使用
设置 rs = 新 ADODB.Recordset
sql = 从[安全]中选择*用户名LIKE'&用户名和 'and Password LIKE'&密码& '
rs.Open sql,Cn,adOpenForwardOnly,adLockReadOnly
如果 不 rs.EOF 然后
validuser = True
否则
validuser = 错误
结束 如果
rs.Close
Set rs = Nothing
Cn.Close
设置 Cn = Nothing
结束 功能
< blockquote>您无法使用早期版本的JET连接到较新版本的Access dbs。你需要使用ACE OLEDB 12.
Provider = Microsoft。 ACE 。 OLEDB 。 12 .0;数据源= C:\ myFolder \ myAccessFile.accdb ;
持久 安全性信息=错误;
AnvilRanger的答案非常好。除此之外,我建议从这里开始:
在Microsoft Visual Basic中使用ADO [ ^ ]
Visual Basic中的ADO代码示例 [ ^ ]
正确的连接字符串,您可以在这里找到: http://www.connectionstrings.com/access/ [< a href =http://www.connectionstrings.com/access/target =_ blanktitle =新窗口> ^ ]
顺便说一下,如查询:
sql =选择*来自[安全]其中用户名LIKE'&用户名和'和密码相似'&密码& '
错了!
为什么?请看这里:与运营商(访问)类似 [< a href =http://office.microsoft.com/en-us/access-help/like-operator-HP001032253.aspxtarget =_ blanktitle =New Window> ^ ]
我建议将以上查询更改为查询,使用参数 [ ^ ]:
PARAMETERS [uname] CHAR ,[pass] CHAR ;
SELECT [用户名],[密码]
FROM [安全性]
WHERE [UserName] = [uname] 和 [密码] = [通过]
如何使用?
声明 ADODB.Command [ ^ ]和< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms677209%28v=vs.85%29.aspx>创建参数(ADO) [ ^ ]将其添加到命令的参数集(ADO) ) [ ^ ]。最后,请致电执行方法(ADO命令) [ ^ ]创建ADODB.recordset
。
MS Access数据库有一些限制,例如保留字列表 [ ^ ],无法使用。
Facts: I am trying to connect VB6 login form to MS access database. The condition is, when i login in the vb login form it supposed to check whether the username and password is correct or not. if correct another form supposed to open which name is mainmenu.
Here the error is unrecognized database format.i don't know why it showing this error because i put the correct file and file extension. Please help me how can i work on this condition. do you have any other idea to connect and check password and username.
'CHECKING Username and password
Public Function validuser(Username As String, password As String)
Dim db As String
Dim Cmd As String
Dim sql As String
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset
db = App.Path & "\nfl.accdb"
Cmd = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db & ""
Set Cn = New ADODB.Connection
With Cn
.ConnectionString = Cmd
.Open
End With
Set rs = New ADODB.Recordset
sql = "Select * From [Security] where Username LIKE '" & Username & "' and Password LIKE '" & password & "'"
rs.Open sql, Cn, adOpenForwardOnly, adLockReadOnly
If Not rs.EOF Then
validuser = True
Else
validuser = False
End If
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Function
You cannot use the earlier versions of JET to connect to newer version of Access dbs. You need to use the ACE OLEDB 12.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; Persist Security Info=False;
AnvilRanger answer is very good. In addition to it, i'd suggest to start here:
Using ADO with Microsoft Visual Basic[^]
ADO Code Examples in Visual Basic[^]
Proper connection string, you'll find here: http://www.connectionstrings.com/access/[^]
By the way, such as query:
sql = "Select * From [Security] where Username LIKE '" & Username & "' and Password LIKE '" & password & "'"
is wrong!
Why? Please, have a look here: Like Operator (Access)[^]
I'd suggest to change above query to query, which uses parameters[^]:
PARAMETERS [uname] CHAR, [pass] CHAR; SELECT [UserName], [Password] FROM [Security] WHERE [UserName] = [uname] and [Password] = [pass]
How to use it?
Declare ADODB.Command[^] and Create Parameter (ADO)[^] to add it to the command's Parameters Collection (ADO)[^]. Finally, call Execute Method (ADO Command)[^] to createADODB.recordset
.
There are some restrictions for MS Access database, like list of reserved words[^], which can't be used.
这篇关于我该如何解决这个错误? "无法识别的数据库格式“ 。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!