Dim objRS As ADODB.Recordset
Dim objCon As ADODB.Connection
Dim getConString As String 'Variable for the connectionstring
Dim sqlProvider As String
Set objCon = New ADODB.Connection
Set objRS = New ADODB.Recordset
getConString = "Provider=SQLOLEDB.1" & _
";Persist Security Info=False" & _
";User ID=用户名" & _
";Password=密码" & _
";Initial Catalog=sqlserver数据库名" & _
";Data Source=服务器名或IP" & _
";Network Library=DBMSSOCN"
GetCon
Private Sub GetCon()
If objCon.State = adStateOpen Then objCon.CloseWith objCon
.ConnectionString = getConString
.Open
End With
End Sub
Sub GetRecordSet(strSource As String)
If objRS.State = adStateOpen Then objRS.Close 'If the database is open close before getting a new recordset
'On Error GoTo Errorhandler 'I take care of eventual errorsSelect Case bolEditReadRS 'Tells which kind of recordset to get
Case False
With objRS
.ActiveConnection = objCon
.CursorLocation = adUseClient
.CursorType = adOpenKeyset 'Move the cursor in any direction and bookable
.LockType = adLockReadOnly 'Editing is not possible
.Source = strSource 'What Recordset to get
.Open
.MoveFirst
End With
Case True
With objRS
.ActiveConnection = objCon
.CursorLocation = adUseClient
.CursorType = adOpenKeyset 'Move the cursor in any direction and bookable
.LockType = adLockOptimistic 'Editing is possible
.Source = strSource 'What Recordset to get
.Open
.MoveFirst
End With
End Select
End Sub
GetRecordSet ("sql语句")
Dim objCon As ADODB.Connection
Dim getConString As String 'Variable for the connectionstring
Dim sqlProvider As String
Set objCon = New ADODB.Connection
Set objRS = New ADODB.Recordset
getConString = "Provider=SQLOLEDB.1" & _
";Persist Security Info=False" & _
";User ID=用户名" & _
";Password=密码" & _
";Initial Catalog=sqlserver数据库名" & _
";Data Source=服务器名或IP" & _
";Network Library=DBMSSOCN"
GetCon
Private Sub GetCon()
If objCon.State = adStateOpen Then objCon.CloseWith objCon
.ConnectionString = getConString
.Open
End With
End Sub
Sub GetRecordSet(strSource As String)
If objRS.State = adStateOpen Then objRS.Close 'If the database is open close before getting a new recordset
'On Error GoTo Errorhandler 'I take care of eventual errorsSelect Case bolEditReadRS 'Tells which kind of recordset to get
Case False
With objRS
.ActiveConnection = objCon
.CursorLocation = adUseClient
.CursorType = adOpenKeyset 'Move the cursor in any direction and bookable
.LockType = adLockReadOnly 'Editing is not possible
.Source = strSource 'What Recordset to get
.Open
.MoveFirst
End With
Case True
With objRS
.ActiveConnection = objCon
.CursorLocation = adUseClient
.CursorType = adOpenKeyset 'Move the cursor in any direction and bookable
.LockType = adLockOptimistic 'Editing is possible
.Source = strSource 'What Recordset to get
.Open
.MoveFirst
End With
End Select
End Sub
GetRecordSet ("sql语句")
无意中浏览此贴,看了你的答复中连接字符串中的一句:Network Library=DBMSSOCN,解决了我的大问题啊。你到此贴来看看,我给你加分:http://expert.csdn.net/Expert/topic/1114/1114939.xml?temp=.495907:(急:有谁做过或试验过连接远程SQL6.5服务器成功的,无论用何种方式(在以C#中),请进来看看,up有分。)
最好对顺便解释下:Network Library=DBMSSOCN这一句的含义,加了这一句后,访问远程SQL6.5的问题解决了(但访问本地数据库则不能加这一句)。在C#中用SQL cilent连接是不需要这一句的。
感谢啊,为了解决这一问题,我发了两个帖子,还参考了许多技术文章,都未提到这一点,对了,你还可以到这来领分:http://expert.csdn.net/Expert/topic/1111/1111021.xml?temp=4.694766E-02,分数一并给你了。