Advertise with us
Control Panel
Forum
Hosting Help
ASP.NET 4.5 & SQL 2012 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
Home
»
Resources
»
Microsoft SQL Server .NET Data Provider (System.Data.SqlClient)
ASP.NET
ADO.NET
Web Services
Remoting
Visual Studio 2005
Error Bank
Interview Questions
Tips & Tricks
XML
HTML
Jscript/Javascript
IIS
Windows
General
Submit Resource or code snippet
... and get
surprise gifts
Win Digital camera, ASP.NET Books, Free softwares!!
Microsoft SQL Server .NET Data Provider (System.Data.SqlClient)
15 Feb, 2009
Author:
amel masinovic
Summary
sqlCon.Open();
...
sqlCon.Close();
.NET Classes used :
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.
Feedbacks about this page from members:
in c#
-
shweta
can i get this code in c#.net
(28 Apr 2010)
phan nay de lam chi rua
-
nguyen tien
Khong biet muc nay de lam chi ha
(23 Jun 2010)
Submit Feedback
View All Feedbacks
Total
members
:
250982
Average new registrations per day (in last 7 days):
8
New Registration:
Open
Register Now
Talk to Webmaster
Tony John
Facebook
Google+
Twitter
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.