Guest User

xTRUMANx

a guest
Feb 23rd, 2010
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. public bool Create(ISchoolObject thing)
  2.         {
  3.             DataTable table = GetTable(thing);
  4.             string tableName = table.TableName;
  5.             // Get all the properties to be stored for the object
  6.             PropertyInfo[] properties = thing.GetType().GetProperties();
  7.            
  8.             // Building InsertCommand
  9.             string columns = "";
  10.             string values = "";
  11.            
  12.             foreach (PropertyInfo property in properties)
  13.             {
  14.                 // Don't need the Id property when creating a *new* record since it is auto-incremented
  15.                 if (property.Name == "Id")
  16.                     continue;
  17.                 string prop = property.Name.ToLower();
  18.                 columns += prop + ", ";
  19.                 values += "@" + prop + ", ";
  20.                 command.Parameters.Add("@" + prop, DbType.String, 0, prop);
  21.             }
  22.             // Next two if statements remove the extra ", "
  23.             if(columns != "")
  24.                 columns = columns.Remove(columns.Length-2);
  25.             if(values != "")
  26.                 values = values.Remove(values.Length-2);
  27.  
  28.             command.CommandText = "INSERT INTO " + tableName + "(" + columns + ") VALUES(" + values + ")";
  29.             adapter.InsertCommand = command;
  30.             // InsertCommand is ready
  31.  
  32.             DataRow row = table.Rows.Add();
  33.             Save(thing, properties, row);
  34.            
  35.             // Setting the Id value of my thing object to the auto-generated id value
  36.             table = GetTable(thing); // This method returns the table in which my object exists in
  37.             int rowCount = table.Rows.Count;
  38.             long id = (long) table.Rows[rowCount - 1]["id"];
  39.             thing.Id = id;
  40.  
  41.             return true;
  42.         }
  43.        
  44.         // This method is used for both creating and updating records
  45.         public bool Save(ISchoolObject thing, PropertyInfo[] properties, DataRow row)
  46.         {
  47.             foreach (PropertyInfo property in properties)
  48.             {
  49.                 // This loops through every property of the object
  50.                 // and then uses the name of the property as the column
  51.                 // name and then calls the get method of the property.
  52.                 // e.g. row["name"] = thing.Name;
  53.                 // Does the above but for every known property automatically
  54.                 // The benefits of learning the power of metaprogramming
  55.                 // from ruby :-)
  56.                 if (property.Name != "Id")
  57.                     row[property.Name.ToLower()] = property.GetGetMethod().Invoke(thing, null);
  58.             }
  59.  
  60.             // This updates the local data set
  61.             adapter.Update(set, GetTableName(thing));
  62.  
  63.             // This actually saves changes to the
  64.             // dataset to the database
  65.             set.AcceptChanges();
  66.  
  67.             // Refilling the dataset so the generated
  68.             // id shows up in the dataset
  69.             RefillDataSet(GetTableName(thing));
  70.  
  71.             return true;
  72.         }
  73.  
  74. public void RefillDataSet(string tableName)
  75.         {
  76.             adapter.SelectCommand.CommandText = "SELECT * FROM " + tableName;
  77.             set.Tables[tableName].Clear();
  78.             adapter.Fill(set, tableName);
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment