| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1283 人关注过本帖
标题:[求助]模块中定义sql数据库连接从ini文件中读取信息
取消只看楼主 加入收藏
Atom
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-9-12
收藏
 问题点数:0 回复次数:2 
[求助]模块中定义sql数据库连接从ini文件中读取信息

程序中模块的定义sql server连接信息如下
Public Const conn As String = "Provider=SQLOLEDB.1;Password=Sa;User ID=sa;Initial Catalog=Student;Data Source=192.168.0.1;"
如果把servername dbtable userid pwd 存放在一个ini文件中,读取后如何把这些值付给上面的表达式?

如果在窗体中用Textbox读取信息后进行付值就可以,但在模块中无法实现,窗体中代码如下:
Conn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + txtUserId.Text + ";Password=" + txtPwd.Text + ";Initial Catalog=" + txtDataBase.Text + ";Data Source=" + txtServerName.Text + ";"

请各位大侠帮帮忙,谢谢!

搜索更多相关主题的帖子: 数据库 ini 模块 sql 定义 
2006-09-12 09:57
Atom
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-9-12
收藏
得分:0 

谢谢楼上的回复,可能我在上面说得不是很清楚,下面我把在窗体中能实现的代码帖上来,但现在我是想在模块中实现同样的功能,初始化数据库连接。

Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Public Conn As New ADODB.Connection


Private Sub Cmd_OK_Click()
Dim success As Long
success = WritePrivateProfileString("Student", "ServerName", txtServerName.Text, App.Path + "\Account.ini")
'参数一、 Section Name 参数二、於.ini中的项目 参数三、项目的内容 参数四 .ini文件的名称
success = WritePrivateProfileString("Student", "DataBase", txtDataBase.Text, App.Path + "\Account.ini")
success = WritePrivateProfileString("Student", "DBLogon", txtDBLogon.Text, App.Path + "\Account.ini")
success = WritePrivateProfileString("Student", "LogonPass", txtLogonPass.Text, App.Path + "\Account.ini")
End Sub

Private Sub Cmd_Cancel_Click()
Call form_load
End Sub

Private Sub Cmd_Exit_Click()
End
End Sub

Private Sub Cmd_Test_Click()
On Error GoTo ErrorDB
Dim strConn As String
Set Conn = New ADODB.Connection
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + txtDBLogon.Text + ";Password=" + txtLogonPass.Text + ";Initial Catalog=" + txtDataBase.Text + ";Data Source=" + txtServerName.Text + ";"
Conn.Open strConn

If Conn.State <> adStateOpen Then
MsgBox "数据库连接失败"
End
Else
MsgBox "数据库连接成功", vbOKOnly, "提示"
End If
Conn.Close
Set Conn = Nothing
Exit Sub

ErrorDB:
If Conn.State = adStateOpen Then Conn.Close
If Not IsNull(Conn) Then Set Conn = Nothing
MsgBox "数据库连接失败", vbOKOnly, "提示"

End Sub

Private Sub form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Student", "ServerName", ".", buff, 256, App.Path + "\Account.ini")
'若.ini Student中无ServerName,则采用参数三的值
txtServerName.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Student", "DataBase", "Student", buff, 256, App.Path + "\Account.ini")
txtDataBase.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Student", "DBLogon", "Sa", buff, 256, App.Path + "\Account.ini")
txtDBLogon.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Student", "LogonPass", " ", buff, 256, App.Path + "\Account.ini")
txtLogonPass.Text = buff
End Sub


Account.ini文件内容
[Student]
ServerName=192.168.0.1
DataBase=Student
DBLogon=Sa
LogonPass=Sa

[此贴子已经被作者于2006-9-12 15:33:41编辑过]

2006-09-12 11:47
Atom
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-9-12
收藏
得分:0 

定义成函数
Dim DBUser As String
...
DBUser = ...
Debug.Print DBUser
...
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & DBUser & ";Password=Sa;Initial Catalog=Student;Data Source=192.168.0.1"
Conn.Open strConn

上面代码中立即窗口显示的值是正确的
但实际是连接不上!晕死!
不能这样表示??User ID=" & DBUser & "
如果直接定上User ID=Sa就可以了

2006-09-12 15:20
快速回复:[求助]模块中定义sql数据库连接从ini文件中读取信息
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017380 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved