菜鸟问题:数据库连接

nowayway 2001-11-26 11:51:09
与远程数据库相连的代码怎么写?能举个例子最好了。
...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihonggen0 2001-11-26
  • 打赏
  • 举报
回复
dim cn as adodb.connect
dim rscn as adodb.recordset
dim connectstring as string
connectstring="driver={SQLSERVER};server=serverip;uid=sa;pwd=;database=userdatabase"
cn.open connectstring

为了防止出错需要修改注册表中的键software\microsoft\mssqlserver\connecttodsquery确保为tcp/ip
lihonggen0 2001-11-26
  • 打赏
  • 举报
回复
Public Function OpenConnection() As String '打开数据库
On Error GoTo SQLConErr
With adoCN
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
.ConnectionTimeout = 15
.Open

If .State = adStateOpen Then
adoDateTime.Open "select getdate()", adoCN, adOpenStatic, adLockOptimistic
cServerDate = Format(adoDateTime(0), "yyyy-mm-dd")
cServertime = Mid(adoDateTime(0), 10)
Else
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End With

SqlCommand.ActiveConnection = adoCN
SqlCommand.CommandType = adCmdText
Exit Function
SQLConErr:
Select Case Err.Number
Case -2147467259
MsgBox "找不到指定的SQL Server服务器或者数据库不存在,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case -2147217843
MsgBox "指定的SQL Server数据库用户不存在或口令错误,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End Select
OpenConnection
End Function
netcar 2001-11-26
  • 打赏
  • 举报
回复
利用ADO进行连结,很方便的。
sqstudio_zsp 2001-11-26
  • 打赏
  • 举报
回复
Public Sub OpenX()

Dim cnn1 As ADODB.Connection
Dim rstEmployees As ADODB.Recordset
Dim strCnn As String
Dim varDate As Variant

' 打开连接。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn

' 打开雇员表。
Set rstEmployees = New ADODB.Recordset
rstEmployees.CursorType = adOpenKeyset
rstEmployees.LockType = adLockOptimistic
rstEmployees.Open "employee", cnn1, , , adCmdTable

' 将第一个雇员记录的受雇日期赋值给变量,然后更改受雇日期。
varDate = rstEmployees!hire_date
Debug.Print "Original data"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date
rstEmployees!hire_date = #1/1/1900#
rstEmployees.Update
Debug.Print "Changed data"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date

' 再查询 Recordset 并重置受雇日期。
rstEmployees.Requery
rstEmployees!hire_date = varDate
rstEmployees.Update
Debug.Print "Data after reset"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date

rstEmployees.Close
cnn1.Close

End Sub

nowayway 2001-11-26
  • 打赏
  • 举报
回复
怎么我给不了分?
rghjf 2001-11-26
  • 打赏
  • 举报
回复
'==============================================================================
'初始化数据环境
'==============================================================================
Function IniDataEnviroment(ByVal sServerNm As String, ByVal sDbNm As String, _
ByVal sUser As String, ByVal sPwd As String) As Long
Dim con As Connection
Dim sConnection As String
On Error GoTo Err_

'打开数据连接、验证
'==============================
For Each con In DE.Connections
With con
.CursorLocation = adUseClient
sConnection = " Provider=SQLOLEDB.1; "

'如果提供了用户名不是用集成安全特性
'==================================
If Trim(sUser) <> "" Then
sConnection = sConnection & " Integrated Security=; "
sConnection = sConnection & " Persist Security Info=FALSE; "
sConnection = sConnection & " Use Procedure for Prepare=1; "
Else
sConnection = sConnection & " Integrated Security=SSPI; "
sConnection = sConnection & " Persist Security Info=False; "
sConnection = sConnection & " Use Procedure for Prepare=1; "
End If

sConnection = sConnection & " Data Source=" & sServerNm & "; "
sConnection = sConnection & " User ID=" & sUser & "; "
sConnection = sConnection & " Password=" & sPwd & "; "

.Properties("Prompt") = 3 '当验证有误时提示用户口令
.ConnectionString = sConnection
.Open
.DefaultDatabase = sDbNm

End With
Next
Set con = Nothing
Exit Function


Err_:
Call SystemEnd
End Function

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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