哪位高手给分析一下下面VB中连接数据库的问题?
'ActiveXDLL的启动程序,为DLL初始化时执行PublicSubDBMain()g_DBPath=App.Path&"\DB\Merchandise.mdb"If...
'ActiveX DLL的启动程序,为DLL初始化时执行
Public Sub DBMain()
g_DBPath = App.Path & "\DB\Merchandise.mdb"
If ConnectToDatabase(DBSQLServer) = False Then
Err.Raise vbObjectError + 1, , "连接数据库出错!"
End If
End Sub
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=merchandise.dsn;UID=sa;PWD="
End Function
'连接到数据库
Public Function ConnectToDatabase(DBType As gxcDBType) As Boolean
On Error GoTo ERR_CONN
Set g_Conn = New Connection
'设置服务器名称,数据库名称,登录名(此时假设密码为空)
Dim ServerName As String, DBName As String, UserName As String, strPwd As String
'这些是为连接Sql Server而用
ServerName = "localhost"
DBName = "Merchandise"
UserName = ""
strPwd = ""
'连接到数据库
With g_Conn
.CursorLocation = adUseClient
.CommandTimeout = 10
If DBType = DBAccess Then
' 连接到ACCESS数据库
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"Data Source=" & g_DBPath
Else
' 连接到SQL Server数据库
.ConnectionString = ConnectString()
End If
.Open
End With
ConnectToDatabase = True
Exit Function
ERR_CONN:
ConnectToDatabase = False
End Function
'替换字符串中的单引号
Public Function RealString(strOrigional) As String
RealString = Replace(strOrigional, "'", "''")
End Function
'得到某个数据表中主键的下一个值,即当前主键值加1
Public Function NextID(ByVal strTable As String, ByVal strId As String) As Long
'两个参数分别是表的名称与主键的名称
Dim rs As Recordset
Set rs = g_Conn.Execute("SELECT MAX(" & strId & ") FROM " & strTable)
If IsNull(rs(0)) Then
'如果值为NULL,则说明无任何数据记录,此时ID应为1
NextID = 1
Else
'使新ID为最大ID值+1
NextID = rs(0).Value + 1
End If
End Function 展开
Public Sub DBMain()
g_DBPath = App.Path & "\DB\Merchandise.mdb"
If ConnectToDatabase(DBSQLServer) = False Then
Err.Raise vbObjectError + 1, , "连接数据库出错!"
End If
End Sub
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=merchandise.dsn;UID=sa;PWD="
End Function
'连接到数据库
Public Function ConnectToDatabase(DBType As gxcDBType) As Boolean
On Error GoTo ERR_CONN
Set g_Conn = New Connection
'设置服务器名称,数据库名称,登录名(此时假设密码为空)
Dim ServerName As String, DBName As String, UserName As String, strPwd As String
'这些是为连接Sql Server而用
ServerName = "localhost"
DBName = "Merchandise"
UserName = ""
strPwd = ""
'连接到数据库
With g_Conn
.CursorLocation = adUseClient
.CommandTimeout = 10
If DBType = DBAccess Then
' 连接到ACCESS数据库
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"Data Source=" & g_DBPath
Else
' 连接到SQL Server数据库
.ConnectionString = ConnectString()
End If
.Open
End With
ConnectToDatabase = True
Exit Function
ERR_CONN:
ConnectToDatabase = False
End Function
'替换字符串中的单引号
Public Function RealString(strOrigional) As String
RealString = Replace(strOrigional, "'", "''")
End Function
'得到某个数据表中主键的下一个值,即当前主键值加1
Public Function NextID(ByVal strTable As String, ByVal strId As String) As Long
'两个参数分别是表的名称与主键的名称
Dim rs As Recordset
Set rs = g_Conn.Execute("SELECT MAX(" & strId & ") FROM " & strTable)
If IsNull(rs(0)) Then
'如果值为NULL,则说明无任何数据记录,此时ID应为1
NextID = 1
Else
'使新ID为最大ID值+1
NextID = rs(0).Value + 1
End If
End Function 展开
展开全部
错误提示是什么提示?找不到数据库?还对象变量和with变量未定义?
更多追问追答
追问
对象变量和with变量未定义QQ1250436259
追答
对象变量和with变量未定义
这个错误就是 你的sql查询语句错了!你好好检查一下(核对一下你数据里的表记录)
好好检查一下这条语句
Set rs = g_Conn.Execute("SELECT MAX(" & strId & ") FROM " & strTable)
strId的值?
strTable的值?
from的前后加空格
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询