| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6921 人关注过本帖
标题:求VB连接SQLSERVER数据库代码
只看楼主 加入收藏
chenzhanpeng
Rank: 1
等 级:新手上路
帖 子:271
专家分:0
注 册:2007-1-5
收藏
 问题点数:0 回复次数:5 
求VB连接SQLSERVER数据库代码
各位大虾:小弟急要VB连接SQLSERVER数据库的代码,并在代码后面加以注释,急用,谢谢你们。。。
搜索更多相关主题的帖子: 数据库 SQLSERVER 代码 注释 
2007-04-28 09:33
kobe412
Rank: 1
来 自:网络的另一边
等 级:新手上路
威 望:1
帖 子:322
专家分:0
注 册:2006-6-12
收藏
得分:0 


Public strSQLServer As String 'SQL服务器地址
Public strSQLUser As String 'SQL用户名
Public strSQLPW As String 'SQL密码
Public strSQLDB As String 'SQL数据库
Public cnMain As New ADODB.Connection '主连接


'连接SQL服务器
Public Function sqlConnect(ByVal cnThis As ADODB.Connection, ByVal strServer As String, ByVal strUser As String, ByVal strPass As String, Optional ByVal strDataBase As String = "")
Dim strSQL As String
'生成连接字符串
strSQL = "provider=sqloledb;server=" & strServer & ";user id=" & strUser & ";password=" & strPass
If strDataBase <> "" Then strSQL = strSQL & ";database=" & strDataBase '如果需要连接到数据库
cnThis.Open strSQL
End Function

'读取SQL服务器配置信息
Public Sub readServer()
On Error GoTo aaaa
Dim strTmp As String, strT() As String
Open GetApp & "Files\sql.inf" For Input As #1
If EOF(1) = False Then Line Input #1, strTmp
Close #1
strTmp = Trim(strTmp)
If strTmp <> "" Then
strT = Split(strTmp, "||")
For i = 0 To 3
strT(i) = strT(i)
Next
strSQLServer = strT(0)
strSQLUser = strT(1)
strSQLPW = strT(2)
strSQLDB = strT(3)
End If
Exit Sub
aaaa:
strSQLServer = ""
strSQLUser = ""
strSQLPW = ""
strSQLDB = ""
End Sub

'保存SQL服务器配置信息
Public Sub SaveServer(ByVal strServer As String, ByVal strUser As String, ByVal strPass As String, ByVal strDataBase)
On Error GoTo aaaa
Open GetApp & "Files\sql.inf" For Output As #1
Print #1, strServer & "||" & strUser & "||" & strPass & "||" & strDataBase
Close #1
Exit Sub
aaaa:
MsgBox "保存 SQL 服务器信息失败!", vbCritical
End Sub


俺这么帅气的人,俺为了挖矿打光棍俺容易吗?
2007-04-28 10:00
kobe412
Rank: 1
来 自:网络的另一边
等 级:新手上路
威 望:1
帖 子:322
专家分:0
注 册:2006-6-12
收藏
得分:0 

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
On Error GoTo aaaa
If txtServer.Text = "" Then
MsgBox "必须填写 SQL 服务器名称或 IP 地址。", vbInformation
txtServer.SetFocus
Exit Sub
End If
If txtUser.Text = "" Then
MsgBox "必须填写 SQL 服务器的用户名。", vbInformation
txtUser.SetFocus
Exit Sub
End If
If txtDB.Text = "" Then
MsgBox "必须填写数据库的名称。", vbInformation
txtDB.SetFocus
Exit Sub
End If
lbCT.Visible = True
DoEvents
Dim cnTest As New ADODB.Connection
sqlConnect cnTest, txtServer.Text, txtUser.Text, txtPW.Text, txtDB.Text
MsgBox "连接 " & txtServer.Text & " 成功!", vbInformation
cnTest.Close
SaveServer txtServer.Text, txtUser.Text, txtPW.Text, txtDB.Text
readServer
Unload Me
Exit Sub
aaaa:
MsgBox Err.Description, vbCritical
lbCT.Visible = False
End Sub

图片附件: 游客没有浏览图片的权限,请 登录注册


俺这么帅气的人,俺为了挖矿打光棍俺容易吗?
2007-04-28 10:06
boboxiong
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2007-4-23
收藏
得分:0 
kobe412 :ADODB.Connection这个是在哪个DAO里面添加的。

2007-10-27 14:53
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
以下是引用boboxiong在2007-10-27 14:53:18的发言:
kobe412 :ADODB.Connection这个是在哪个DAO里面添加的。

ADO。

2007-10-27 18:56
zzyzzg
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-6-15
收藏
得分:0 
keyikankname ?
2013-04-23 13:08
快速回复:求VB连接SQLSERVER数据库代码
数据加载中...
 
   



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

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