C# - The SqlConnection Class

The SqlConnection class provides all the properties and methods needed to make a connection to a Microsoft SQL database.

 

Properties and Methods of the SqlConnection Class

Item Description
Properties
ConnectionString Contains the database connection string
ConnectionTimeout Contains the timeout for the connection in seconds
Database Contains the name of the database to connect to
DataSource Contains the name of the connected server
PacketSize Contains the size of packets used to communicate with the server
ServerVersion Contains the version of SQL that the server is running
State Contains the state of the connection
WorkstationId Contains the NetBIOS identifier of the machine hosting the Web form
Methods
BeginTransaction() Places the connection into a transaction and returns the newly created SqlTransaction object
ChangeDatabase() Enables the developer to change to a different database programmatically
Close() Closes the connection to the current database
CreateCommand() Returns a new command object
Open() Opens the connection to the database
SqlConnection.ConnectionString

Syntax

String ConnectionString

Description

The ConnectionString property contains the database connection string.

Example

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost; 
            User Id=sa; Password=; Initial Catalog=northwind";
SqlConnection.ConnectionTimeout

Syntax

Int32 ConnectionTimeout

Description

ConnectionTimeout is a read-only property that contains the length of time in seconds before the connection times out. If you need to modify the length of time before the connection times out, change the Connection Timeout setting in your connection string. The default for this property is 30 seconds

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.ConnectionTimeout;
SqlConnection.Database

Syntax

String Database

Description

Database is a read-only property that contains the name of the connected database. To change databases, you need to use the ChangeDatabase() method or change the connection string.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.Database;
SqlConnection.DataSource

Syntax

String DataSource

Description

DataSource is a read-only property that contains the name of the data source you are connecting to. To change the data source, you need to modify your connection string.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.DataSource;
SqlConnection.PacketSize

Syntax

Int32 PacketSize

Description

PacketSize is a read-only property that contains the size of packets used in communication with the server.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.PacketSize;
SqlConnection.ServerVersion

Syntax

String ServerVersion

Description

ServerVersion is a read-only property that contains the version number of SQL server that the server is running.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.ServerVersion;
SqlConnection.State

Syntax

ConnectionState State

Description

The State property contains detailed information about the current state of the connection. It can contain the following values: Broken, Closed, Connecting, Executing, Fetching, and Open. These values can be found in the System.Data.ConnectionState namespace.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.State.ToString();
SqlConnection.WorkstationId

Syntax

String WorkstationId

Description

The WorkstationId property contains the NetBIOS name of the computer originating the SQL connection.

Example

SqlConnection conn = new SqlConnection();
msg.Text = conn.WorkstationId;
SqlConnection.BeginTransaction()

Syntax

SqlTransaction BeginTransaction( IsolationLevel iso )
SqlTransaction BeginTransaction()
SqlTransaction BeginTransaction( IsolationLevel iso, String transactionName )
SqlTransaction BeginTransaction( String transactionName)

Description

The BeginTransaction() method places the current connection under a transaction.

Example

SqlConnection conn = new SqlConnection("Data Source=localhost; 
         User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();

SqlTransaction trans = conn.BeginTransaction(IsolationLevel.Serializable, "RemoveProducts");
SqlConnection.ChangeDatabase()

Syntax

Boolean ChangeDatabase( String database )

Description

The ChangeDatabase() method enables a developer to change databases programmatically, without having to create a new connection object.

Example

SqlConnection conn = new SqlConnection("Data Source=localhost; 
          User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();

conn.ChangeDatabase("test"); //Database now changed to 'test'
SqlConnection.Close()

Syntax

Void Close()

Description

The Close() method closes the connection to the database.

Example

SqlConnection conn = new SqlConnection("Data Source=localhost; 
         User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();

conn.Close();
SqlConnection.CreateCommand()

Syntax

SqlCommand CreateCommand()

Description

The CreateCommand() method returns a new instance of a SqlCommand object.

Example

SqlConnection conn = new SqlConnection("Data Source=localhost;
         User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM Products";
SqlConnection.Open()

Syntax

Void Open()

Description

The Open() method opens the connection to the database by using the information provided in the ConnectionString property.

Example

SqlConnection conn = new SqlConnection("Data Source=localhost;
         User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
 
Visitor Review  Write a review
Search Review :

jagdeesh raghuwanshi on 21 Mar 2013 03:06 reply

GoooooooooooooooooD

 

rachit on 12 Jun 2012 02:56 reply

this was help me a lot on d first da of my training in dot.net

 

adnan on 22 Mar 2012 03:55 reply

very very good ALA

 

xcv on 29 Feb 2012 06:19 reply

drop database triconsole

 

welber on 08 Feb 2012 12:30 reply

simple C# connectionstring sample

http://asp.net-informations.com/ado.net/asp-connection-string.htm

wel.

 

KAVITHA BHARATHI on 19 Jun 2011 06:08 reply

really superb

kailash on 20 Dec 2011 04:54 reply

nice representation
 

daniel on 27 May 2011 05:59 reply

nice examples..............

 

saj on 08 Feb 2011 02:32 reply

good

 

Mehmet on 15 Dec 2010 05:28 reply

really nice expression

 

robert on 28 Jul 2010 02:35 reply

so bad explanations

rawan on 13 Jan 2012 09:15 reply

hmmmmmmmmm
 

dzero on 22 Jul 2010 16:33 reply

Thanks lot.This artical is realy help to me.Thanks again.

 

NASIB DAR on 30 Jun 2010 01:52 reply

Its better for all .net programmar
thanks a lot!

 

dilip on 04 Jan 2010 23:41 reply

its good for all asp.net profiler thanks

 

Rajeev Mishra on 28 Aug 2009 22:38 reply

Short and understandandable

 

bala on 26 Aug 2009 09:12 reply

Hi,Thank u very much.

better to cover all methods .

 

Saif on 20 Aug 2009 09:57 reply

Nice One it is very Rare to find such a Description on the net.
Thanks a lot...

 

Ramkesh Gurjar on 18 Aug 2009 05:20 reply

Great you are very Intellegent

 

Alladin on 22 Jun 2009 03:08 reply

Excellently explained all the things. The ServerVersion should have checked after opening the connection.

 

Sasi on 05 Jun 2009 11:23 reply

thank you ...
For interview preparation this article is very good.

 

Matthew Johns on 20 May 2009 23:03 reply

Thank you, It really Helped AAA

 

suiraj on 23 Apr 2009 07:35 reply

Thanks for U r Notes

 

Anuj Saini on 20 Feb 2009 13:41 reply

time efficient and good description

 

Rohit Mathur on 18 Dec 2008 13:15 reply

Never do such a wasteful attemp to explain this rubbish thing to developers !

It's the worst contain that i ve ever read

Matthew Johns on 20 May 2009 23:01 reply

Rohit,

You are an idiot, there are many developers out there, such as myself, who benefits from this.

Just because you think it is a waste of time, does'nt mean it is. Either way, who died and made you God.

Oh, and don't take out your frustartion here if you wasted your own time in reading something you already understand.

You are a bafoon %P%
arjun on 09 Mar 2010 01:40 reply

u r the JOKER

ipwnyou on 09 Jul 2009 23:11 reply

Yup, why dont you write your own article? If you dont like to read it, piss off, nobody cares about you and your lousy comments. Grow up much?

rakesh on 18 Feb 2011 16:21 reply

no one forcing u to read this ok... if u dont like shut ur ass off ok....
 

Raghavendran on 16 Dec 2008 10:28 reply

Very Very Very Very Very Very Very Very Good

 

GUNA on 06 Oct 2008 03:50 reply

VERY GOOD.

VERY SHORT, GOOD

 
See all reviews
Write a review
Rating:
Your Message:
Name:
Email:
(optional)
(Your email that entered here will not show anywhere on website and will use only reference when someone reply your post)
  I want to receive a copy of email on this post (your email is needed)
 
Code:
 

Search this site