Understanding database access using ADO.NET in c#

DATABASE ACCESS WITH ADO NET

C# Tutorials

  

DATABASE ACCESS WITH ADO NET

Learn Tutorials
Understanding database access using ADO.NET in c#

Example code for Database access with ADO NET:
basic database access with ADO.NET


//using System.Data.SqlClient - optimized for SQL server
//using System.Data.OleDb - optimized for MSaccess, Excel, dBase
//using System.Data.Odbc - for odbc datasources set up in console
//using System.Data.OracleClient - for Oracle databases


private void accessDB()
{
   //create connection obj
   SqlConnection connObj = new SqlConnection();
   connObj.ConnectionString =    @"server=www.yourdbservernamehere.com;Database=FirstProtect;uid=;pwd=";
   
   //create SqlDataReader object to receive the data from the database
   SqlDataReader dr;

   //create SqlCommand object and set its properties below
   SqlCommand cmd = new SqlCommand();

   //set the sql statement as the CommandText property of the SqlCommand obj
   cmd.CommandText="SELECT fname,lname FROM testtable";

   //set the Connection property of the SqlCommand obj
   cmd.Connection = connObj;

   //open the connection
   connObj.Open();

   //call the ExecuteReader method of the Command obj to execute the command
   dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

   //dump the dataset returned
   while (dr.Read())
   {
   Console.WriteLine(dr.GetString(0)+" "+dr.GetString(1));
   }

   //close the Connection obj
   connObj.Close();

}
This is example code for understanding database access using ado.net in c#