次の方法で共有


SqlException.Source プロパティ

エラーを生成したプロバイダの名前を取得します。

Overrides Public ReadOnly Property Source As String
[C#]
public override string Source {get;}
[C++]
public: __property String* get_Source();
[JScript]
public override function get Source() : String;

プロパティ値

エラーを生成したプロバイダの名前。

解説

これは、 Errors プロパティ内にある最初の SqlErrorSource プロパティのラッパーです。

使用例

[Visual Basic, C#, C++] SqlErrorCollection コレクション内の各 SqlError を表示する例を次に示します。

 
Public Sub ShowSqlException()
     Dim mySelectQuery As String = "SELECT column1 FROM table1"
     Dim myConnection As New SqlConnection ("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;")
     Dim myCommand As New SqlCommand(mySelectQuery, myConnection)

     Try
         myCommand.Connection.Open()
     Catch e As SqlException
         Dim errorMessage As String = "Message: " & e.Message & vbCrLf & _
                                      "Source: " & e.Source & vbCrLf & _
                                      "State: " & e.State & vbCrLf & _
                                      "Procedure: " & e.Procedure & vbCrLf

        Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
        log.Source = "My Application"
        log.WriteEntry(errorMessage)
        Console.WriteLine("An exception occurred. Please contact your system administrator.")
     End Try
End Sub

[C#] 
public void ShowSqlException() 
 {
    string mySelectQuery = "SELECT column1 FROM table1";
    SqlConnection myConnection =
       new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;");
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

    try 
    {
       myCommand.Connection.Open();
    }
    catch (SqlException e) 
    {
      string errorMessage = "Message: " + e.Message + "\n" +
                            "Source: " + e.Source + "\n" +
                            "State: " + e.State + "\n" +
                            "Procedure: " + e.Procedure + "\n";

      System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
      log.Source = "My Application";
      log.WriteEntry(errorMessage);
      Console.WriteLine("An exception occurred. Please contact your system administrator.");
    }
}

[C++] 
public:
void ShowSqlException() 
 {
    String* mySelectQuery = S"SELECT column1 FROM table1";
    SqlConnection* myConnection =
       new SqlConnection(S"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;");
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);

    try 
    {
       myCommand->Connection->Open();
    }
    catch (SqlException* e) 
    {
      String* errorMessage = String::Concat(
         String::Format( S"Message: {0}\nSource: {1}\n", e->Message, e->Source ),
         String::Format( S"State: {0}\nProcedure: {1}\n", __box(e->State), e->Procedure ));

      System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
      log->Source = S"My Application";
      log->WriteEntry(errorMessage);
      Console::WriteLine(S"An exception occurred. Please contact your system administrator.");
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

SqlException クラス | SqlException メンバ | System.Data.SqlClient 名前空間 | Number | State | Class | Server | Message | Procedure | LineNumber