Export (0) Print
Expand All

SqlConnection Constructor

.NET Framework 1.1

Initializes a new instance of the SqlConnection class.

Overload List

Initializes a new instance of the SqlConnection class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public SqlConnection();
[C++] public: SqlConnection();
[JScript] public function SqlConnection();

Initializes a new instance of the SqlConnection class when given a string containing the connection string.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public SqlConnection(string);
[C++] public: SqlConnection(String*);
[JScript] public function SqlConnection(String);

Example

[Visual Basic, C#, C++] The following example creates and opens a SqlConnection.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the SqlConnection constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub CreateSqlConnection()
    Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30"
    Dim myConnection As New SqlConnection(myConnectString)
    myConnection.Open()
End Sub 'CreateSqlConnection

[C#] 
public void CreateSqlConnection() 
 {
    string myConnectString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";
    SqlConnection myConnection = new SqlConnection(myConnectString);
    myConnection.Open();
 }

[C++] 
public:
void CreateSqlConnection() 
 {
    String* myConnectString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";
    SqlConnection* myConnection = new SqlConnection(myConnectString);
    myConnection->Open();
 }

[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.

See Also

SqlConnection Class | SqlConnection Members | System.Data.SqlClient Namespace

Show:
© 2014 Microsoft