Basic Database Operations in ADO.NET

This article will show the basic database transactions select, insert update, and delete. I am using the MS-Sql database northwind for the demonstration. I am not going to use the class SqlCommandBuilder and I will tell you the reason later.
Here are the steps:
  1. Establish a connection to the SQL server
    1. string strConnection="user id=sa;password=mozart;";   
    2. strConnection+="database=northwind;server=frankfurt";   
    3. SqlConnection objSqlConnection = new SqlConnection(strConnection);  
    4. objSqlConnection.Open();  
  2. Create a Dataset.( A DataSet  is  a snapshot  cache from the database.)
    1. DataSet objDataSet = new DataSet("OrderAdmin");  
  3. Fill the table, which you want to work with it.
    a) Create a SELECT SqlCommand and a SqlDataAdapter 
    b) Map the table to SqlDataAdapter
    c) connect the SqlCommand and the SqlDataAdapter 
    d) Fill the DataSet
    1. // Retrive data from the table Order Detail   
    2. string strOrderDetails = "SELECT * FROM [Order Details]";   
    3. SqlCommand objOrdDetailCommand = new SqlCommand(strOrderDetails,objSqlConnection);   
    4. SqlDataAdapter objODAdapter = new SqlDataAdapter();   
    5. objODAdapter.TableMappings.Add("Table","Order Details"); objODAdapter.SelectCommand = objOrdDetailCommand;   
    6. /*SqlCommandBuilder objODCB = new SqlCommandBuilder(objODAdapter);*/ objODAdapter.Fill(objDataSet);  
  4. Change the DataSet  and Update with the database
    Now, for example, you want  update a data row in the table "[Order Details]" and you make a change in the Dataset and call the method Update method.
    This action will cause an error because DataSet is not able to generate SqlCommands. So you have to supply an appropriate SQL statement to the SqlDataAdapter. 
    a) Update the DataRow
    b) Create a suitable SQLCommand  
    c) Connect the SQLCommand and the SqlDataAdapter
    d) Update the DataSet 
    1. // Update the record of the table Orders, which has OrderID= 10248   
    2. foreach(DataRow objRow in objOrdTable.Rows) {  
    3.       string strID = objRow["OrderID"].ToString();  
    4.       string strShip = "HMS Invisible";  
    5.       if (strID == "10248") {  
    6.             string strUpDate = "UPDATE Orders SET ShipName= " + "'" + strShip + "'";  
    7.             strUpDate += " WHERE OrderID = " + strID;  
    8.             objRow["ShipName"] = strShip;  
    9.             objOrdAdapter.UpdateCommand = new SqlCommand(strUpDate, objSqlConnection);  
    10.             objOrdAdapter.Update(objDataSet, "Orders");  
    11.             break;  
    12.       }  
    13. }  
You can follow the same pattern to delete, insert, and add transactions. 
  1. You dont need to supply a suitable SqlCommand to the SqlDataAdapter, if you use SqlCommandBuilder.  But the use of the class SqlCommandBuilder is restricted. It is not able to supply SqlCommands if the tables have a relationship within a DataSet. 
  2. You must give an appropriate SqlCommand to the SqlDataAdapter, otherwise, you will damage the table. For example, you have deleted only one data row in the DataSet and you supply
    a SqlCommand like this
    1. objODAdapter.DeleteCommand = new SqlCommand("DELETE FROM [Order Details]")  
The SqlAdapter will then delete all the records in the table. In other words, Dataset is not able to check the SqlCommand.