我用我自己的机器做服务器又做客户端,在本机上测试可以正常连接
如果到局域网的其他机器上怎么连接呢?
用IP可以连接吗?还有就是需要做什么设置?我杂本机测试时,不知道为什么不需要用户名和密码就可正常连接(我第一次用SQL当初建数据库时,我是用ACCESS数据库直接导入到SQL2000 的),现在问题是要是在其他机器上连接我的数据库,我的机器作为服务器来用,需要怎么连接,怎么设置?
请各位大哥大姐赐教
非常感谢
如果到局域网的其他机器上怎么连接呢?
用IP可以连接吗?还有就是需要做什么设置?我杂本机测试时,不知道为什么不需要用户名和密码就可正常连接(我第一次用SQL当初建数据库时,我是用ACCESS数据库直接导入到SQL2000 的),现在问题是要是在其他机器上连接我的数据库,我的机器作为服务器来用,需要怎么连接,怎么设置?
请各位大哥大姐赐教
非常感谢
你使用IP來聯接當然是可以的,也可以用電腦名<當此電腦上只安裝了一個實例的時候>
"Provider=sqloledb;Data Source=192.168.1.11;Initial Catalog=" & "csdb" & ";User Id=sa;Password=1234"
用dsn.手工设置以下odbc.或者用代码建立dsn.或者采用非dsn连接.
代码建dsn
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongSub CreatDSN()Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "Microsoft Access Driver (*.mdb)"----------这里要修改成sqlserver
'null 结尾的参数
'请看相关文档
'strAttributes = "SERVER=SomeServer" & Chr$(0)
'strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)'这里要修改strAttributes = strAttributes & "DSN=tsgl" & Chr$(0)'这里要修改strAttributes = strAttributes & "DBQ=" & App.Path & "\dbase.mdb" & Chr$(0)'这里要修改
'strAttributes = strAttributes & "UID=admin" & Chr$(0) ''这里要修改'strAttributes = strAttributes & "PWD=" & Chr$(0)'这里要修改'如果要显示对话,可使用 Form1.Hwnd 代替 vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Sub以上是在我的软件里copy的.access的dsn代码建立用sqlserver 要改以下eeeeeee
ConnectString = "driver={SQL Server};server=" & 服务器名称 & ";uid=用户名;pwd=密码;database=数据库"
End FunctionPublic 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
以上放在模块中;
在程序中:Dim txtSQL As String
Dim MsgText As String
Dim ADOname As ADODB.RecordsettxtSQL = "select * from 表"
Set ADOname = ExecuteSQL(txtSQL, MsgText)