1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| public class MappingDao { public static int SetTableObject<T>(MySqlConnection conn,T tableObject) { string TableStr = tableObject.GetType().Name; string FieldStr = ""; string ValueStr = ""; foreach (System.Reflection.PropertyInfo info in tableObject.GetType().GetProperties()) { if(info.GetValue(tableObject,null) != null) { string field = info.Name; object value = info.GetValue(tableObject, null);
if (value.GetType() == typeof(string)) { if (!value.ToString().Equals("")) { FieldStr += (field + ","); ValueStr += ("'" + value + "',"); } } else if (value.GetType() == typeof(decimal)) { FieldStr += (field + ","); ValueStr += ("'" + value + "',"); } else if (value.GetType() == typeof(DateTime)) { DateTime dateTime = new DateTime(0001, 01, 01); if (DateTime.Compare(dateTime, (DateTime)value) < 0) { FieldStr += (field + ","); ValueStr += ("'" + value + "',"); } } } } FieldStr = FieldStr.Substring(0, FieldStr.Length - 1); ValueStr = ValueStr.Substring(0, ValueStr.Length - 1); string sql = "insert into " + TableStr + " (" + FieldStr + ") values (" + ValueStr + ")"; MySqlCommand cmd = new MySqlCommand(sql, conn); return cmd.ExecuteNonQuery(); } public static List<T> GetTableObjects<T>(MySqlConnection conn, string sql) { List<T> tLst = new List<T>(); MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { var instance = Activator.CreateInstance<T>(); foreach (System.Reflection.PropertyInfo info in instance.GetType().GetProperties()) { if (string.IsNullOrEmpty(reader.GetValue(reader.GetOrdinal(info.Name)).ToString())) { continue; } info.SetValue(instance, reader.GetValue(reader.GetOrdinal(info.Name)), null); } tLst.Add(instance); } reader.Close(); return tLst; }
public static int RemoveFrameRecordByNO(MySqlConnection conn,string frmNo) { string sql = "delete from azhtfb where CHAMAC='" + frmNo + "'"; MySqlCommand cmd = new MySqlCommand(sql, conn); return cmd.ExecuteNonQuery(); }
public static MySqlConnection GetMySqlConnection(string connStr) { MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); return conn; } catch (MySqlException ex) { throw ex; } } }
|