The Microsoft SQL Server .NET Data Provide allows you to connect to a Microsoft SQL Server 7.0, 2000, and 2005 databases.
For Microsoft SQL Server 6.5 or earlier, use the OLE DB .NET Data Provider with the SQL Server OLE DB Provider (SQLOLEDB).
Using C#:
using System.Data.SqlClient; ... SqlConnection oSQLConn = new SqlConnection(); oSQLConn.ConnectionString = "Data Source=(local);" + "Initial Catalog=myDatabaseName;" + "Integrated Security=SSPI"; //Or // "Server=(local);" + // "Database=myDatabaseName;" + // "Trusted_Connection=Yes"; oSQLConn.Open(); ... oSQLConn.Close(); // If you open the connection, then close the connection! // Otherwise the connection does not go back into the connection pool. // Note the SqlDataAdapter will open and close the connection for you // when calling it's Fill or Update methods. However if the connection // is already open, the SqlDataAdapter will leave it open.
Using VB.NET:
Imports System.Data.SqlClient ... Dim oSQLConn As SqlConnection = New SqlConnection() oSQLConn.ConnectionString = _ "Data Source=(local);" & _ "Initial Catalog=myDatabaseName;" & _ "Integrated Security=SSPI" oSQLConn.Open()
If connection to a remote server (via IP address):
oSQLConn.ConnectionString = _ "Network Library=DBMSSOCN;" & _ "Data Source=xxx.xxx.xxx.xxx,1433;" & _ "Initial Catalog=myDatabaseName;" & _ "User ID=myUsername;" & _ "Password=myPassword" oSQLConn.Open()Where:
- "Network Library=DBMSSOCN" tells SqlClient to use TCP/IP Q238949
- xxx.xxx.xxx.xxx is an IP address of the remote SQL Server.
- 1433 is the port number for the remote SQL Server. Q269882 and Q287932
- You can also add "Encrypt=yes" for encryption
For more information, see: SqlConnection Class, Q308656, and .NET Data Providers
To view Microsoft KB articles related to SQLClient, click here
Note: Microsoft SQLXML Managed Classes exposes the functionality of SQLXML inside the Microsoft .NET Framework.
浙公网安备 33010602011771号