2009-08-02 11 views
5

私は以下のモジュールを持っており、私は接続をテストしたいと思います。どのように私は接続が動作するかどうかをテストするのですか?あなたがあなたの答えと非常に具体的に説明してくださいすることができますvb.netを介してSQL DBに接続しているかどうかをテストするにはどうすればよいですか?

Imports System.Data.SqlClient 

Module Module1 
    Sub Main() 
     ' Create a new SqlConnectionStringBuilder and 
     ' initialize it with a few name/value pairs: 
     Dim builder As New SqlConnectionStringBuilder(GetConnectionString()) 

     ' The input connection string used the 
     ' Server key, but the new connection string uses 
     ' the well-known Data Source key instead. 
     Console.WriteLine(builder.ConnectionString) 

     ' Pass the SqlConnectionStringBuilder an existing 
     ' connection string, and you can retrieve and 
     ' modify any of the elements. 
     builder.ConnectionString = _ 
      "server=http://sql.example.com;user id=******;" & _ 
      "password=***********;" 
     ' Now that the connection string has been parsed, 
     ' you can work with individual items. 
     Console.WriteLine(builder.Password) 
     builder.Password = "[email protected]" 
     builder.AsynchronousProcessing = True 

     ' You can refer to connection keys using strings, 
     ' as well. When you use this technique (the default 
     ' Item property in Visual Basic, or the indexer in C#) 
     ' you can specify any synonym for the connection string key 
     ' name. 
     builder("Server") = "." 
     builder("Connect Timeout") = 1000 

     ' The Item property is the default for the class, 
     ' and setting the Item property adds the value to the 
     ' dictionary, if necessary. 
     builder.Item("Trusted_Connection") = True 
     Console.WriteLine(builder.ConnectionString) 

     Console.WriteLine("Press Enter to finish.") 
     Console.ReadLine() 
    End Sub 

    Private Function GetConnectionString() As String 
     ' To avoid storing the connection string in your code, 
     ' you can retrieve it from a configuration file. 
     Return "Server=(local);Integrated Security=SSPI;" & _ 
      "Initial Catalog=AdventureWorks" 
    End Function 
End Module 

答えて

8

あなたは、接続文字列を持った後に開いて、あなたはConnection.Open()を使用して接続を開くことができます。あなたはConnection.Close()との接続を閉じる前に、任意の時点で、ここ

connection.enterコードを開こうとすると あなたはConnection.Stateを使用して、その状態を確認することができ、発生したエラーをキャッチするtry-catchブロック内にそれを行うことができます。これは、列挙型の値を返します(ConnectionState.Openなど)。

+0

このように、接続を開くだけで、接続できることが確認されています – PsychoData

2

は、接続オブジェクトを作成し、それが

関連する問題