Answer: 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