使用例
Cnn1SQLLでは、SQL Server Providerを使用してMSDE に接続します。
Cnn1aSQLでは、SQL Server Providerを使用してMSDE に接続します。
Cnn2SQLでは、ODBC Provider(DSN-less)を使用してMSDEに接続します。
Cnn3SQLでは、ODBC Provider(DSN-based)を使用してMSDEに接続します。
Cnn4SQLでは、Link Fileを使用してMSDEに接続します。
Sub Cnn1SQL()
Dim cnn As New ADODB.Connection
' Using a SQL Server provider to connect to NorthwindCS.
With cnn
.Open "Provider=SQLOLEDB;" & _
"Integrated Security=SSPI;" & _
"Data Source=DELLXPS;" & _
"Initial Catalog=NorthwindCS"
.Close
End With
MsgBox "Cnn1SQL OK"
End Sub
Sub Cnn1aSQL()
Dim cnn As New ADODB.Connection
' Using a SQL Server provider to connect to NorthwindCS.
With cnn
.Open "Provider=SQLOLEDB;" & _
"Data Source=DELLXPS;" & _
"Initial Catalog=NorthwindCS;" & _
"User ID=sa;Password="
.Close
End With
MsgBox "Cnn1aSQL OK"
End Sub
Sub Cnn2SQL()
Dim cnn As New ADODB.Connection
' Using a ODBC provider to connect to NorthwindCS (DSN-less).
With cnn
.Open "Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=DELLXPS;" & _
"Database=NorthwindCS;" & _
"UID=;PWD="
.Close
End With
MsgBox "Cnn2SQL OK"
End Sub
Sub Cnn3SQL()
Dim cnn As New ADODB.Connection
' Using a ODBC provider to connect to NorthwindCS (DSN-based).
With cnn
.Open "Provider=MSDASQL;" & _
"DSN=NorthwindCS"
.Close
End With
MsgBox "Cnn3SQL OK"
End Sub
Sub Cnn4SQL()
Dim cnn As New ADODB.Connection
' Using a Data Link File to connect to NorthwindCS.
With cnn
.Open "File Name=D:\SQLServer.UDL"
Debug.Print .ConnectionString
.Close
End With
MsgBox "Cnn4SQL OK"
End Sub
|
Tip1:
Cnn1SQLでは、MSDEにログインするときWindows NTの統合セキュリティを使用していますので、"Integrated Security=SSPI "が必要になります。
システム管理者(System Administrator:sa)のユーザー名でログインするときは、"Integrated
Security=SSPI"を"User ID=sa;Password=???"で置換します
Tip2:
MSDEのデータベース(.MDF)に接続するには、SQL ServerのOLEDB providerまたはODBC providerを使用します。
この場合、ConnectionオブジェクトのOpenメソッドで指定するConnectionStringが異なります。
Openメソッドで指定するConnectionStringがよく解らないときは、Cnn4SQLで紹介したデータリンクファイル(.UDL)を作成して、Debug.printでConnectionStringプロパティを表示させると便利です。
データリンクファイル(.UDL)は、エクスプローラの<ファイル>メニューから<新規作成>をクリックして<Microsoftデータリンク>を選択して作成します。