What is the Use of Connection Object ?

 Posted by Chvrsri on 1/6/2011 | Category: ADO.NET Interview questions | Views: 9696 | Points: 40

Inorder to interact with any database we need a Connection . This connection enables us to know the Server Name, User-Id,Password,Database Name. These are the main requirements to connect to a Database.

So in our Connection string we provide all those above mentioned properties as parameters.

Different Connection objects : SqlConnection, OleDbConnection, OdbcConnection

. OleDbConnection object is used with an OLE-Db Provider

OleDbConnection cnn ;
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=DatabaseName;";
cnn = new OleDbConnection(connetionString);

. SqlConnection object used the Tabular Data Services (TDS) With MS Sql Server.

SqlConnection con;
con = new SqlConnection("server=SERVER NAME;user id=ID;password=PASSWORD;database=DB NAME");
SqlCommand cmd;
cmd = new SqlCommand("DB Query Here",con);

. Using the OdbcConnection , we will create an OdbcCommand. From that command we can issue a Query and create an OdbcDataReader.

OdbcConnection DbConnection = new OdbcConnection("Connection String");
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText = "Db Query Here";
OdbcDataReader DbReader = DbCommand.ExecuteReader();

Source: My Own !!! | Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response