VB如何连接SQL SERVER数据库?

auhey 2002-12-12 04:24:50
以前写的连接都是针对SQL SERVER身份验证的,写成
"Data Provider=SQLOLEDB.1;" _
& "Persist Security Info=False;" _
& "User ID=sa;" _
& "Password=;" _
& "Initial Catalog=dbname;" _
& "Data Source=dbserver"
现在碰到一个注册,连接是以WINDOWS身份验证的方式登录数据库的,这个时候连接字串应该怎么写呀?
急!在线等待,请各位帮忙!
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
auhey 2002-12-12
  • 打赏
  • 举报
回复
多谢ZHAOZHIF!搞定了
ZHAOZHIF 2002-12-12
  • 打赏
  • 举报
回复
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mis;Data Source=计算机名
previewer1024 2002-12-12
  • 打赏
  • 举报
回复
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "Driver={SQL Server};Server=(local);UID=;PWD=;Database=" & DataBaseName

End Function
写好的SQL调用 ExecuteSql()就可以了
auhey 2002-12-12
  • 打赏
  • 举报
回复
对了,还有一个小问题,Presist Security Info这个值是干什么的呀?只知道写成True或者False都能连,还不知道是什么意思呢。

7,763

社区成员

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

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