Export (0) Print
Expand All

SqlConnection.DataSource Property

Gets the name of the instance of SQL Server to which to connect.

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

Property Value

The name of the instance of SQL Server to which to connect. The default value is an empty string.

Example

[Visual Basic, C#, C++] The following example creates a SqlConnection and displays some of its read-only properties.

[Visual Basic] 
Public Sub CreateSqlConnection()
    Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;data source=mySQLServer"
    Dim myConnection As New SqlConnection(myConnectString)
    myConnection.Open()
    MessageBox.Show("ServerVersion: " & myConnection.ServerVersion & ControlChars.NewLine & _
                    "Datasource: " & myConnection.DataSource)
    myConnection.Close()
End Sub 'CreateSqlConnection

[C#] 
public void CreateSqlConnection() 
 {
    string myConnectString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlConnection myConnection = new SqlConnection(myConnectString);
    myConnection.Open();
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion +
                    "\nDatasource: " + myConnection.DataSource);
    myConnection.Close();
 }

[C++] 
public:
void CreateSqlConnection() 
 {
    String* myConnectString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlConnection* myConnection = new SqlConnection(myConnectString);
    myConnection->Open();
    MessageBox::Show(String::Format( S"ServerVersion: {0}\nDatasource: {1}", myConnection->ServerVersion, myConnection->DataSource ));
    myConnection->Close();
 }

[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

SqlConnection Class | SqlConnection Members | System.Data.SqlClient Namespace | ConnectionString | ConnectionTimeout | Database

Show:
© 2014 Microsoft