关于VB6数据库连接和类模块问题
PublicSqlConnAsADODB.ConnectionPublicsConnAsStringPublicMeIdAsStringPublicSubOpenConn...
Public SqlConn As ADODB.Connection
Public sConn As String
Public MeId As String
Public Sub OpenConn()
If SqlConn Is Nothing = True Then
'建立数据库连接对象
Set SqlConn = New ADODB.Connection
End If
If SqlConn.State <> 1 Then
'打开数据库连接
SqlConn.Open sConn
End If
End Sub
Public Sub CloseConn()
'如果数据库连接对象不为空则关闭数据库连接
If SqlConn.State = 1 Then
SqlConn.Close
End If
End Sub
Public Function RunSelectSQL(ByVal sSQLString As String) As ADODB.Recordset
Dim rs As New Recordset
'打开数据库连接
Me.OpenConn
'执行SQL操作
rs.Open sSQLString, SqlConn, 1, 3
Set RunSelectSQL = rs
End Function
Private Sub Class_Initialize()
sConn = "Provider=SQLOLEDB;Data Source='" & MeID & "';DATABASE=Mybase;UID=sa;pwd=aa"
End Sub
本人初学vb6,遇到数据库难题,上面是类代码,调用时需要赋两个变量,sSQLString 和MeID,如新窗体调用时:
Dim db as new databases, sql as string
Sql=”select * from AAA”
MeID=”P001” ‘P001为局域网主机名
Set mshflexgird1.datasource = db. RunSelectSQL(Sql)
‘问题出现了,在P001这台机上面执行正常,但换了台局域网机却找不到数据库,确定是出现在MeID上,但本人不会更改,请大师帮忙。
MeID 是可以更改的,可通过text1.text更改,不是固定不变的。 展开
Public sConn As String
Public MeId As String
Public Sub OpenConn()
If SqlConn Is Nothing = True Then
'建立数据库连接对象
Set SqlConn = New ADODB.Connection
End If
If SqlConn.State <> 1 Then
'打开数据库连接
SqlConn.Open sConn
End If
End Sub
Public Sub CloseConn()
'如果数据库连接对象不为空则关闭数据库连接
If SqlConn.State = 1 Then
SqlConn.Close
End If
End Sub
Public Function RunSelectSQL(ByVal sSQLString As String) As ADODB.Recordset
Dim rs As New Recordset
'打开数据库连接
Me.OpenConn
'执行SQL操作
rs.Open sSQLString, SqlConn, 1, 3
Set RunSelectSQL = rs
End Function
Private Sub Class_Initialize()
sConn = "Provider=SQLOLEDB;Data Source='" & MeID & "';DATABASE=Mybase;UID=sa;pwd=aa"
End Sub
本人初学vb6,遇到数据库难题,上面是类代码,调用时需要赋两个变量,sSQLString 和MeID,如新窗体调用时:
Dim db as new databases, sql as string
Sql=”select * from AAA”
MeID=”P001” ‘P001为局域网主机名
Set mshflexgird1.datasource = db. RunSelectSQL(Sql)
‘问题出现了,在P001这台机上面执行正常,但换了台局域网机却找不到数据库,确定是出现在MeID上,但本人不会更改,请大师帮忙。
MeID 是可以更改的,可通过text1.text更改,不是固定不变的。 展开
3个回答
展开全部
MeID=”P001” 把主机名改下呗。
更多追问追答
追问
这个我试过可以,因有好20多个不同服务器用,我不能每处都改,再制成exe,那太麻烦了,另外也不能通用,我的意思是拷到别处,直接通过窗口赋值给meid即可运行
追答
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim meid As String, a As Long
meid = String(32, 0)
a = GetComputerName(meid, 32)
MsgBox meid
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询