Advertisement
wingman007

OdbcTest

Nov 17th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Odbc;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. // Set the Properties -> Build to x64 bit
  10. // The ODBC driver is 64 bit
  11.  
  12. namespace OdbcTest
  13. {
  14.     public class Person // : IId
  15.     {
  16.         public int Id { get; set; }
  17.         public string Name { get; set; }
  18.         public string FamilyName { get; set; }
  19.         public DateTime BirthDate { get; set; }
  20.         public string Address { get; set; }
  21.  
  22.         public override string ToString()
  23.         {
  24.             return string.Format("{0},{1},{2},{3},{4}",
  25.                 Id, Name,
  26.                 FamilyName,
  27.                 BirthDate.ToString("yyyy-MM-dd"),
  28.                 Address);
  29.         }
  30.     }
  31.  
  32.     class Program
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.  
  37.             var _connection = new OdbcConnection(@"DSN=OrmEdd;uid=ormedd;pwd=ormedd;DATABASE=ormedd");
  38.             List<Person> persons = new List<Person>();
  39.  
  40.             try
  41.             {
  42.                 #region Preparation can be outside of try
  43.                 IDbCommand command = _connection.CreateCommand();
  44.                 command.Connection = _connection;
  45.                 command.CommandText = @"SELECT * FROM `Persons`";
  46.                 #endregion
  47.  
  48.                 _connection.Open();
  49.                 //Perform DB operation here i.e. any CRUD operation
  50.                 using (IDataReader reader = command.ExecuteReader())
  51.                 {
  52.                     while (reader.Read())
  53.                     {
  54.                         persons.Add(Hydrate(reader));
  55.                     }
  56.                 }
  57.             }
  58.             catch (Exception e)
  59.             {
  60.                 // Log
  61.                 throw e;
  62.             }
  63.             finally
  64.             {
  65.                 _connection.Close();
  66.             }
  67.  
  68.             foreach(Person person in persons)
  69.             {
  70.                 Console.WriteLine(person);
  71.             }
  72.  
  73.         }
  74.  
  75.  
  76.         private static Person Hydrate(IDataReader reader)
  77.         {
  78.             Person person = new Person();
  79.             // It was working for GetAll with Id
  80.             person.Id = (int)reader["ID"]; //  Id System.InvalidOperationException: No data exists for the row/column.
  81.             person.Name = (string)reader["Name"];
  82.             person.FamilyName = (string)reader["FamilyName"];
  83.             // I don't need this System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.
  84.             // person.BirthDate = DateTime.Parse((string)reader["BirthDate"]); // we don't need cast
  85.             person.BirthDate = (DateTime)reader["BirthDate"]; // but we still need a cast
  86.             person.Address = (string)reader["Address"];
  87.             return person;
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement