我是写到注册表中,然后读取出来,连接数据库 Global Const AppNameStr = "WHTransApp" Global Const SectionStr = "SystemSetting" Global Const CnnDSN = "UseDSN" Global Const CnnPAR = "UsePAR"Global Const CnnTypeKey = "CnnType" Global Const DSNKey = "DSNName" Global Const SrvKey = "ServerName" Global Const DBKey = "DataBaseName" Global Const UidKey = "UserName" Global Const PwdKey = "Password" Global Const strProgramName = "通用报表系统" Global BillCount As Integer Global Cn As New ADODB.Connection Global Rs As New ADODB.RecordsetPublic rpt As ClsFreeRpt '表类Sub Main() FrmMain.Show Call ConnectDB End Sub Function GetCnnStr() As String
Select Case GetSetting(AppNameStr, SectionStr, CnnTypeKey) Case CnnDSN GetCnnStr = "DSN=" & GetSetting(AppNameStr, SectionStr, DSNKey) Case CnnPAR GetCnnStr = "DRIVER=SQL Server;" GetCnnStr = GetCnnStr & "Server=" & GetSetting(AppNameStr, SectionStr, SrvKey) & ";" GetCnnStr = GetCnnStr & "DataBase=" & GetSetting(AppNameStr, SectionStr, DBKey) & ";" GetCnnStr = GetCnnStr & "Uid=" & GetSetting(AppNameStr, SectionStr, UidKey) & ";" GetCnnStr = GetCnnStr & "Pwd=" & GetSetting(AppNameStr, SectionStr, PwdKey) & ";" End Select End FunctionFunction ConnectDB() As BooleanOn Error GoTo errHandle Dim CnnStr As String
If Cn.State = adStateOpen Then If MsgBox("现在数据库已处于连接状态,如果要继续进行会丢失现有连接,是否继续?", vbQuestion + vbYesNo) = vbNo Then Exit Function Else Cn.Close End If End If
End SubPrivate Sub Form_Load() '从注册表中得到现有连接情况 Select Case GetSetting(AppNameStr, SectionStr, CnnTypeKey) Case CnnDSN OptDSN.Value = True OptParamt.Value = Not OptDSN.Value
Case CnnPAR OptDSN.Value = False OptParamt.Value = Not OptDSN.Value End Select
Private objDataLink As New MSDASC.DataLinks
Dim sConnect As String
Set objDataLink = New MSDASC.DataLinks
sConnect = objDataLink.PromptNew
你试试,很好用还方便
http://sanjianxia.myrice.com/vb/116.htm
Global Const AppNameStr = "WHTransApp"
Global Const SectionStr = "SystemSetting"
Global Const CnnDSN = "UseDSN"
Global Const CnnPAR = "UsePAR"Global Const CnnTypeKey = "CnnType"
Global Const DSNKey = "DSNName"
Global Const SrvKey = "ServerName"
Global Const DBKey = "DataBaseName"
Global Const UidKey = "UserName"
Global Const PwdKey = "Password"
Global Const strProgramName = "通用报表系统"
Global BillCount As Integer
Global Cn As New ADODB.Connection
Global Rs As New ADODB.RecordsetPublic rpt As ClsFreeRpt '表类Sub Main()
FrmMain.Show
Call ConnectDB
End Sub
Function GetCnnStr() As String
Select Case GetSetting(AppNameStr, SectionStr, CnnTypeKey)
Case CnnDSN
GetCnnStr = "DSN=" & GetSetting(AppNameStr, SectionStr, DSNKey)
Case CnnPAR
GetCnnStr = "DRIVER=SQL Server;"
GetCnnStr = GetCnnStr & "Server=" & GetSetting(AppNameStr, SectionStr, SrvKey) & ";"
GetCnnStr = GetCnnStr & "DataBase=" & GetSetting(AppNameStr, SectionStr, DBKey) & ";"
GetCnnStr = GetCnnStr & "Uid=" & GetSetting(AppNameStr, SectionStr, UidKey) & ";"
GetCnnStr = GetCnnStr & "Pwd=" & GetSetting(AppNameStr, SectionStr, PwdKey) & ";"
End Select
End FunctionFunction ConnectDB() As BooleanOn Error GoTo errHandle
Dim CnnStr As String
If Cn.State = adStateOpen Then
If MsgBox("现在数据库已处于连接状态,如果要继续进行会丢失现有连接,是否继续?", vbQuestion + vbYesNo) = vbNo Then
Exit Function
Else
Cn.Close
End If
End If
CnnStr = GetCnnStr
Cn.Open CnnStr
ConnectDB = True
FrmMain.StatusBar1.Panels(1).Text = "服务器连接状态 成功"
Exit Function
errHandle:
MsgBox "数据库连接错误,请进行连接检查。", vbInformation, "系统错误"
ConnectDB = False
FrmMain.StatusBar1.Panels(1).Text = "服务器连接状态 失败"
FrmCnnSet.Show vbModal
End Function
你可以不使用ODBC而直接在程序中写连接语句,为什么不用ADODB呢,用起来可方便呢!
例如: '连接 本地数据库(Access2000数据库)得到程序所需资料清单
Set MyConnectLoca = New Connection
With MyConnectLoca
.CursorLocation = adUseClient
.ConnectionTimeout = 120
.CommandTimeout = 60
.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & YouFilePath & ";Jet OLEDB:Database Password=youpassword"
End With'这样就可以连到Access2000数据库了,如果连接SQL Server数据库_
可以用
' .Open "PROVIDER=MSDASQL;driver={SQL Server};server=" & ServerName & ";uid=" & LogonUsr & ";pwd=" & LogonPwd & ";database=" & DatabaseName & ";Address=" & ServerAddressIP & ";"
'不就可以了吗,将ODBC设置 删掉吧!二、
从你的问题上看,我觉得你可以做一个用户权限库(Access2000/97的)然后将相应设置写入权限数据库中,通过程序直接调用不就可以了吗!
连接权限数据库的方法同(问题一);你的问题就全解决了!!!不是吗!
设置连接 Private Sub CmdChkCnn_Click()
'检测连接是否成功
On Error GoTo errHandle
Dim TmpCn As New ADODB.Connection
Dim TmpCnnStr As String
If OptDSN.Value Then
TmpCnnStr = "DSN=" & TxtDSN.Text
Else
TmpCnnStr = "DRIVER=SQL Server;"
TmpCnnStr = TmpCnnStr & "Server=" & TxtSrv.Text & ";"
TmpCnnStr = TmpCnnStr & "DataBase=" & TxtDB.Text & ";"
TmpCnnStr = TmpCnnStr & "Uid=" & TxtUid.Text & ";"
TmpCnnStr = TmpCnnStr & "Pwd=" & TxtPwd.Text & ";"
End If
TmpCn.Open TmpCnnStr
If TmpCn.State = adStateOpen Then
MsgBox "连接成功。", vbInformation, "系统设置"
End If
TmpCn.Close
Exit Sub
errHandle:
MsgBox "连接失败。", vbInformation, "系统设置"
End SubPrivate Sub cmdSave_Click()
'保存到注册表中
Dim CnnTypeStr As String
If OptDSN.Value Then
CnnTypeStr = CnnDSN
Else
CnnTypeStr = CnnPAR
End If
SaveSetting AppNameStr, SectionStr, CnnTypeKey, CnnTypeStr
SaveSetting AppNameStr, SectionStr, DSNKey, TxtDSN.Text
SaveSetting AppNameStr, SectionStr, SrvKey, TxtSrv.Text
SaveSetting AppNameStr, SectionStr, DBKey, TxtDB.Text
SaveSetting AppNameStr, SectionStr, UidKey, TxtUid.Text
SaveSetting AppNameStr, SectionStr, PwdKey, TxtPwd.Text
MsgBox "保存完毕。", vbInformation, "系统设置"
End SubPrivate Sub Form_Load()
'从注册表中得到现有连接情况
Select Case GetSetting(AppNameStr, SectionStr, CnnTypeKey)
Case CnnDSN
OptDSN.Value = True
OptParamt.Value = Not OptDSN.Value
Case CnnPAR
OptDSN.Value = False
OptParamt.Value = Not OptDSN.Value
End Select
TxtDSN.Text = GetSetting(AppNameStr, SectionStr, DSNKey)
TxtSrv.Text = GetSetting(AppNameStr, SectionStr, SrvKey)
TxtDB.Text = GetSetting(AppNameStr, SectionStr, DBKey)
TxtUid.Text = GetSetting(AppNameStr, SectionStr, UidKey)
TxtPwd.Text = GetSetting(AppNameStr, SectionStr, PwdKey)
End Sub