VB如何从INI文件中读取用户名,密码,服务器IP地址等等信息,然后利用这些信息连接SQL数据库

sunjie521 2010-03-08 10:17:52
请问VB如何从INI文件中读取用户名,密码,数据库,服务器IP地址等等信息,然后利用这些信息连接SQL数据库

我创建一个INI文件,与工程同目录
里面的内容是我在VB工程模块里连接SQL数据库的一段语句。
strconn=Provider=SQLOLEDB.1;PASSWORD=sa;Persist security info=true;User ID=sa;Initial Catalog=zlsa;Data Source=192.168.1.1

这样SQL连接数据库就固定在程序里了,我想通过修改INI文件的内容方便其他用户连接服务器数据库。 那么这个INI文件的内容该怎么写呢?
...全文
318 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
guyehanxinlei 2010-03-08
  • 打赏
  • 举报
回复
这样的代码找一下一大堆的
jabulin 2010-03-08
  • 打赏
  • 举报
回复
参考代码:
Dim strTextLine As String
Dim strArray() As String
Dim StrDsn As String
Dim strDBUserID As String
Dim strDBPassword As String
Dim strcom As String
Dim BolDBConn As Boolean
Dim rs As Object

If Not FileExists(App.Path & "\tool.ini") Then
MsgBox "Can't find tool.ini!", vbInformation, "Information Message:"
End If
If Not FileExists(App.Path & "\Initialize.log") Then
MsgBox "Can't find Initialize.log!", vbInformation, "Information Message:"
End If

Open App.Path & "\tool.ini" For Input As #1

Do While Not EOF(1)
Line Input #1, strTextLine
strTextLine = Trim$(strTextLine)
If Left(strTextLine, 3) = "con" Then
strTextLine = Right(strTextLine, Len(strTextLine) - 4)
strArray = Split(strTextLine, ";")
StrDsn = strArray(0)
strDBUserID = strArray(1)
strDBPassword = strArray(2)
Else
strcom = Right(strTextLine, Len(strTextLine) - 4)
End If
Loop
Close #1


ini文件格式如下:
con:CONSTRING;ID;PASSWORD
com:820000
chinaboyzyq 2010-03-08
  • 打赏
  • 举报
回复
以下内容格式供参考:(setup.ini)
[serverName]
name=jujumao
[serverIP]
ip=192.168.1.1
[userID]
id=sa
[PassWrd]
wrd=sa
[serverPrt]
prt=6000
读ini文件的方式
http://topic.csdn.net/u/20100308/10/1118793b-cd9c-4643-9eef-e083c81dd3a8.html?54742

这些读回来后,你自己利用它们组连接串即可(用 & 连接变量)
sunjie521 2010-03-08
  • 打赏
  • 举报
回复
感谢楼上,能否说的具体一些,最好写出代码,我是个初学者,还不太懂
threenewbee 2010-03-08
  • 打赏
  • 举报
回复
建议对字符串编码,因为"="是不能直接出现的。
GetPrivateProfileString() API
threenewbee 2010-03-08
  • 打赏
  • 举报
回复
建议对字符串编码,因为"="是不能直接出现的。
GetPrivateProfileString() API

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧