C# ADO.NET Connection

Posted on Updated on

C# ADO.NET Connection

The Connection Object is a piece of ADO.NET Data Provider and it is a novel session with the Data Source. The Connection Object is Handling the piece of physical correspondence between the C# application and the Data Source. The Connection Object associate with the predefined Data Source and open an association between the C# application and the Data Source, relies on upon the parameter determined in the Connection String . At the point when the association is built up, SQL Commands will execute with the assistance of the Connection Object and recover or control information in the Data Source. Once the Database action is over , Connection ought to be shut and discharge the Data Source assets .

In C# the sort of the Connection is rely on upon which Data Source framework you are working with. The accompanying are the generally utilized Connections from the C# applications.

C# SQL Server Connection

You can associate your C# application to information in a SQL Server database utilizing the .NET Framework Data Provider for SQL Server. The initial phase in a C# application is to make a case of the Server object and to build up its association with an example of Microsoft SQL Server. The SqlConnection Object is Handling the piece of physical correspondence between the C# application and the SQL Server Database . A case of the SqlConnection class in C# is upheld the Data Provider for SQL Server Database. The SqlConnection occasion takes Connection String as contention and pass the worth to the Constructor articulation.

Sql Server connection string

connetionString=”Data Source=ServerName; Initial

Catalog=DatabaseName;User

ID=UserName;Password=Password”

On the off chance that you have a named example of SQL Server, you’ll have to include that too. At the point when the association is set up , SQL Commands will execute with the assistance of the Connection Object and recover or control the information in the database. Once the Database exercises is over , Connection ought to be shut and discharge the Data Source assets .

The Close() method in SqlConnection Class is used to close the Database Connection. The Close method rolls back any pending transactions and releases the Connection from the SQL Server Database.

using System;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

string connetionString = null;

SqlConnection cnn ;

connetionString = “Data Source=ServerName;Initial Catalog=DatabaseName;User

ID=UserName;Password=Password”

cnn = new SqlConnection(connetionString);

try

{

cnn.Open();

MessageBox.Show (“Connection Open ! “);

cnn.Close();

}

catch (Exception ex)

{

MessageBox.Show(“Can not open connection ! “);

}

}

}

}

C# ADO.NET Connection

The Connection Object is a piece of ADO.NET Data Provider and it is an exceptional session with the Data Source. The Connection Object is Handling the piece of physical correspondence between the C# application and the Data Source. The Connection Object associate with the predefined Data Source and open an association between the C# application and the Data Source, relies on upon the parameter determined in the Connection String . At the point when the association is built up, SQL Commands will execute with the assistance of the Connection Object and recover or control information in the Data Source. Once the Database action is over, Connection ought to be shut and discharge the Data Source assets .In C# the type of the Connection is depend on which Data Source system you are working with.

C# OLEDB Connection

The C# OleDbConnection occurrence takes Connection String as contention and pass the quality to the Constructor articulation. An example of the C# OleDbConnection class is upheld the OLEDB Data Provider .

connetionString = “Provider=Microsoft.Jet.OLEDB.4.0; Information Source=yourdatabasename.mdb;”; cnn = new OleDbConnection(connetionString);

At the point when the association is built up between C# application and the predetermined Data Source, SQL Commands will execute with the assistance of the Connection Object and recover or control information in the database. Once the Database exercises is over Connection ought to be shut and discharge from the information source assets .

cnn.Close();

The Close() strategy in the OleDbConnection class is utilized to close the Database Connection. The Close strategy Rolls Back any pending exchanges and discharges the Connection from the Database associated by the OLEDB Data Provider.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s