Export (0) Print
Expand All

SqlException.Server Property

Gets the name of the computer running an instance of SQL Server that generated the error.

[Visual Basic]
Public ReadOnly Property Server As String
[C#]
public string Server {get;}
[C++]
public: __property String* get_Server();
[JScript]
public function get Server() : String;

Property Value

The name of the computer running an instance of SQL Server.

Remarks

This is a wrapper for the Server property of the first SqlError in the Errors property.

Example

[Visual Basic, C#, C++] The following example displays each SqlError within the SqlErrorCollection collection.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

SqlException Class | SqlException Members | System.Data.SqlClient Namespace | Number | State | Source | Class | Message | Procedure | LineNumber

Show:
© 2014 Microsoft