Advertisement
smaction

Misha Connection

Oct 18th, 2020
3,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        // Load the records from the Horses database table into an in-memory List<>
  2.         private void LoadData()
  3.         {
  4.             const string connectionString = @"Provider=sqloledb; Data Source=(local)\SQLExpress; Integrated Security=SSPI; Integrated Security=SSPI; Initial Catalog=Horses";
  5.  
  6.             // this method uses the OleDb libraries to connect to a server;
  7.             // there are other ways to handle database connectivity; this was
  8.             // among the first for .NET applications.
  9.  
  10.             // The general steps are as follows:
  11.             //      1.  Create and open a database Connection
  12.             //      2.  Create and configure a Command for that connection
  13.             //      3.  Use a DataAdapter to execute the command and fill a DataSet
  14.             //      4.  Loop through the records of the table in the populated dataset
  15.             //           to "convert" each data record into a Horse object
  16.  
  17.             // prepare for the final step by creating an initial empty list of horses
  18.             _horseRecords = new List<HorseRecord>();
  19.  
  20.             // Now Step 1 - We'll create the connection within a 'using' block.  When the block completes,
  21.             // the connection will automatically be disposed gracefully this way;
  22.             using (var connection = new OleDbConnection(connectionString))    
  23.             {
  24.                 connection.Open();
  25.  
  26.                 // Step 2 - create a Command object for executing a query against the database
  27.                 using (var command = connection.CreateCommand())
  28.                 {
  29.                     command.CommandType = CommandType.Text;     // indicates SQL text
  30.                     command.CommandText = "SELECT name, finish, start FROM Horses";
  31.  
  32.                     // Step 3 - use a DataAdapter to execute the command and fill a DataSet
  33.                     var dataset = new DataSet();
  34.                     var dataAdapter = new OleDbDataAdapter(command);
  35.  
  36.                     dataAdapter.Fill(dataset);
  37.  
  38.                     // Step 4 - loop through the records of the table in the populated dataset
  39.                     //   to convert each data record into a Horse object
  40.                     for (var i = 0; i < dataset.Tables[0].Rows.Count; i++)
  41.                     {
  42.                         var row = dataset.Tables[0].Rows[i];
  43.                         var horse = new HorseRecord();
  44.                         horse.Name = row["name"].ToString();
  45.                         horse.Finish = Convert.ToInt32(row["finish"]);
  46.                         horse.Start = Convert.ToInt32(row["start"]);
  47.  
  48.                         // add the horse to our in-memory List<>
  49.                         _horseRecords.Add(horse);
  50.                     }
  51.                 }
  52.  
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement