Results 1 to 12 of 12

Thread: Retrieving and Saving Data in Databases

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Retrieving and Saving Data in Databases

    VB version here.

    There is all sorts of literature on this topic but people still keep asking the same questions. I'm creating this thread so I can send people here to look at some example code that I know will demonstrate all the principles they need. These principles can be extended or adjusted and applied to any data access situation. This code uses members of the System.Data.SqlClient namespace. If you're not using SQL Server then it's a simple matter of switching to the corresponding types of the appropriate namespace for your data source. For example, if you're using an Access database then you'd use an OleDb.OleDbConnection rather than an SqlClient.SqlConnection.

    Retrieving a single value. The ExecuteScalar method returns the value from the first column of the first row of the query's result set:
    CSharp Code:
    1. using (SqlConnection connection = new SqlConnection("connection string here"))
    2. using (SqlCommand command = new SqlCommand("SELECT SUM(Quantity) FROM StockItem",
    3.                                            connection))
    4. {
    5.     connection.Open();
    6.  
    7.     double totalQuantity = (double)command.ExecuteScalar();
    8. }
    Retrieving multiple records that will be read and discarded. The ExecuteReader method provides read-only, forward-only access to the entire result set:
    CSharp Code:
    1. using (SqlConnection connection = new SqlConnection("connection string here"))
    2. using (SqlCommand command = new SqlCommand("SELECT Quantity, Unit, Name FROM StockItem",
    3.                                            connection))
    4. {
    5.     connection.Open();
    6.  
    7.     using (SqlDataReader reader = command.ExecuteReader())
    8.     {
    9.         while (reader.Read())
    10.         {
    11.             MessageBox.Show(String.Format("There are {0} {1} of {2} remaining in stock.",
    12.                                           reader["Quantity"],
    13.                                           reader["Unit"],
    14.                                           reader["Name"]));
    15.         }
    16.     }
    17. }
    Retrieving multiple records for display that will not be updated. The DataTable.Load method will populate a DataTable with the result set exposed by a DataReader:
    CSharp Code:
    1. using (SqlConnection connection = new SqlConnection("connection string here"))
    2. using (SqlCommand command = new SqlCommand("SELECT Quantity, Unit, Name FROM StockItem",
    3.                                            connection))
    4. {
    5.     connection.Open();
    6.  
    7.     using (SqlDataReader reader = command.ExecuteReader())
    8.     {
    9.         DataTable table = new DataTable();
    10.          
    11.         table.Load(reader);
    12.              
    13.         // The table can be used here to display the data.
    14.         // That will most likely be done via data-binding but that is NOT a data access issue.
    15.     }
    16. }
    Retrieving multiple records for display and editing, then saving the changes. The DataAdapter.Fill method populates a DataTable with the contents of the result set of a query. The DataAdapter.Update method saves the changes in a DataTable in accordance with the SQL statements contained in the DeleteCommand, InsertCommand and UpdateCommand properties:
    CSharp Code:
    1. private SqlConnection connection = new SqlConnection("connection string here");
    2. private SqlDataAdapter adapter;
    3. private DataTable table = new DataTable();
    4.  
    5. private void InitialiseDataAccessObjects()
    6. {
    7.     this.adapter = new SqlDataAdapter("SELECT ID, Name, Quantity, Unit FROM StockItem", this.connection);
    8.  
    9.     SqlCommand delete = new SqlCommand("DELETE FROM StockItem WHERE ID = @ID", this.connection);
    10.     SqlCommand insert = new SqlCommand("INSERT INTO StockItem (Name, Quantity, Unit) VALUES (@Name, @Quantity, @Unit)", this.connection);
    11.     SqlCommand update = new SqlCommand("UPDATE StockItem SET Name = @Name, Quantity = @Quantity, Unit = @Unit WHERE ID = @ID", this.connection);
    12.      
    13.     delete.Parameters.Add("@ID", SqlDbType.Int, 4, "ID");
    14.      
    15.     insert.Parameters.Add("@Name", SqlDbType.VarChar, 100, "Name");
    16.     insert.Parameters.Add("@Quantity", SqlDbType.Float, 8, "Quantity");
    17.     insert.Parameters.Add("@Unit", SqlDbType.VarChar, 10, "Unit");
    18.      
    19.     update.Parameters.Add("@Name", SqlDbType.VarChar, 100, "Name");
    20.     update.Parameters.Add("@Quantity", SqlDbType.Float, 8, "Quantity");
    21.     update.Parameters.Add("@Unit", SqlDbType.VarChar, 10, "Unit");
    22.     update.Parameters.Add("@ID", SqlDbType.Int, 4, "ID");
    23.      
    24.     this.adapter.DeleteCommand = delete;
    25.     this.adapter.InsertCommand = insert;
    26.     this.adapter.UpdateCommand = update;
    27.      
    28.     this.adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    29. }
    30.  
    31. private void GetData()
    32. {
    33.     // Retrieve the data.
    34.     this.adapter.Fill(this.table);
    35.      
    36.     // The table can be used here to display and edit the data.
    37.     // That will most likely involve data-binding but that is not a data access issue.
    38. }
    39.  
    40. private void SaveData()
    41. {
    42.     // Save the data.
    43.     this.adapter.Update(this.table);
    44. }
    Note that if your query involves only one table and it has a primary key then you can take the easy option and use a CommandBuilder instead of creating the non-query commands yourself:
    CSharp Code:
    1. private SqlConnection connection = new SqlConnection("connection string here");
    2. private SqlDataAdapter adapter;
    3. private SqlCommandBuilder builder;
    4. private DataTable table = new DataTable();
    5.  
    6. private void InitialiseDataAccessObjects()
    7. {
    8.     this.adapter = new SqlDataAdapter("SELECT ID, Name, Quantity, Unit FROM StockItem", this.connection);
    9.     this.builder = new SqlCommandBuilder(this.adapter);
    10.      
    11.     this.adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    12. }
    13.  
    14. private void GetData()
    15. {
    16.     // Retrieve the data.
    17.     this.adapter.Fill(this.table);
    18.      
    19.     // The table can be used here to display and edit the data.
    20.     // That will most likely involve data-binding but that is not a data access issue.
    21. }
    22.  
    23. private void SaveData()
    24. {
    25.     // Save the data.
    26.     this.adapter.Update(this.table);
    27. }
    Saving changes directly to one or more records in the database. The Command.ExecuteNonQuery method will execute any SQL statement and not return a result set. It can be used to execute a query but you'd never use it for that unless you were populating a view or temp table. Usually you'd use ExecuteNonQuery to execute a DELETE, INSERT or UPDATE command:
    CSharp Code:
    1. using (SqlConnection connection = new SqlConnection("connection string here"))
    2. using (SqlCommand command = new SqlCommand("INSERT INTO StockItem (Name, Quantity, Unit) VALUES (@Name, @Quantity, @Unit)",
    3.                                            connection))
    4. {
    5.     command.Parameters.AddWithValue("@Name", someName);
    6.     command.Parameters.AddWithValue("@Quantity", someQuantity);
    7.     command.Parameters.AddWithValue("@Unit", someUnit);
    8.  
    9.     connection.Open();
    10.  
    11.     command.ExecuteNonQuery();
    12. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width