經過長時間的。net開發,發現每次操作數據這塊,很繁瑣,要麼是直接拼接sql語句,要麼是利用實體類,但是還是需要拼接sql語句,如何找到一個能減少拼接sql語句的並且還能用到實體類數據的方法呢,我在一直摸索。於是得出了一下方法:、
現舉例示範代碼如下,當然我覺得還可以改進(比如重複的地方還可以寫成公共方法進行調用)。
- /// <summary>
- /// 通過泛型插入數據
- /// </summary>
- /// <typeparam name="T">類名稱</typeparam>
- /// <param name="obj">類對象,如果要插入空值,請使用@NULL</param>
- /// <returns>插入的新記錄ID</returns>
- public static int Insert<T>(T obj)
- {
- StringBuilder strSQL = new StringBuilder();
- strSQL = GetInsertSQL(obj);
- // 插入到數據庫中
- object result = SQLPlus.ExecuteScalar(CommandType.Text, strSQL, null);
- return Convert.IsDBNull(result) ? 0 : Convert.ToInt32(result);
- }
- /// <summary>
- /// 通過泛型更新數據
- /// </summary>
- /// <typeparam name="T">類名稱</typeparam>
- /// <param name="obj">類對象,如果要更新空值,請使用@NULL</param>
- /// <returns>更新結果,大於0爲更新成功</returns>
- public static int Update<T>(T obj)
- {
- StringBuilder strSQL = new StringBuilder();
- strSQL = GetUpdateSQL(obj);
- if (String.IsNullOrEmpty(strSQL.ToString()))
- {
- return 0;
- }
- // 更新到數據庫中
- object result = SQLPlus.ExecuteNonQuery(CommandType.Text, strSQL, null);
- int returnValue = Convert.IsDBNull(result) ? 0 : Convert.ToInt32(result);
- return returnValue;
- }
- /// <summary>
- /// 獲取實體的插入語句
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="obj">實體對象</param>
- /// <returns>返回插入語句</returns>
- public static StringBuilder GetInsertSQL<T>(T obj)
- {
- string tableKey = GetPropertyValue(obj, BaseSet.PrimaryKey);
- string keyValue = GetPropertyValue(obj, tableKey);
- string tableName = GetPropertyValue(obj, BaseSet.TableName);
- Type t = obj.GetType();//獲得該類的Type
- StringBuilder strSQL = new StringBuilder();
- strSQL.Append("insert into " + tableName + "(");
- string fields = "";
- string values = "";
- //再用Type.GetProperties獲得PropertyInfo[]
- foreach (PropertyInfo pi in t.GetProperties())
- {
- object name = pi.Name;//用pi.GetValue獲得值
- // 替換Sql注入符
- string value1 = Convert.ToString(pi.GetValue(obj, null)).Replace("'", "''");
- //string dataType = pi.PropertyType.ToString().ToLower();
- string properName = name.ToString().ToLower();
- if (!string.IsNullOrEmpty(value1) && properName != tableKey.ToLower() && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
- {
- // 判斷是否爲空
- if (value1 == BaseSet.NULL)
- {
- value1 = "";
- }
- fields += Convert.ToString(name) + ",";
- values += "'" + value1 + "',";
- }
- }
- // 去掉最後一個,
- fields = fields.TrimEnd(',');
- values = values.TrimEnd(',');
- // 拼接Sql串
- strSQL.Append(fields);
- strSQL.Append(") values (");
- strSQL.Append(values);
- strSQL.Append(")");
- strSQL.Append(";SELECT @@IDENTITY;");
- return strSQL;
- }
- /// <summary>
- /// 獲取實體的更新SQL串
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="obj">實體對象</param>
- /// <returns>返回插入語句</returns>
- private static StringBuilder GetUpdateSQL<T>(T obj)
- {
- string tableKey = GetPropertyValue(obj, BaseSet.PrimaryKey);
- string keyValue = GetPropertyValue(obj, tableKey);
- string tableName = GetPropertyValue(obj, BaseSet.TableName);
- StringBuilder strSQL = new StringBuilder();
- if (string.IsNullOrEmpty(keyValue))
- {
- return strSQL;
- }
- Type t = obj.GetType();//獲得該類的Type
- strSQL.Append("update " + tableName + " set ");
- string subSQL = "";
- string condition = " where " + tableKey + "='" + keyValue.Replace("'", "''") + "'";
- //再用Type.GetProperties獲得PropertyInfo[]
- foreach (PropertyInfo pi in t.GetProperties())
- {
- object name = pi.Name;//用pi.GetValue獲得值
- // 替換Sql注入符
- string value1 = Convert.ToString(pi.GetValue(obj, null)).Replace("'", "''");
- //string dataType = pi.PropertyType.ToString().ToLower();
- string properName = name.ToString().ToLower();
- if (!string.IsNullOrEmpty(value1) && properName != tableKey.ToLower() && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
- {
- // 判斷是否爲空
- if (value1 == BaseSet.NULL)
- {
- value1 = "";
- }
- subSQL += Convert.ToString(name) + "='" + value1 + "',";
- }
- }
- // 去掉最後一個,
- subSQL = subSQL.TrimEnd(',');
- // 拼接上更新子句
- strSQL.Append(subSQL);
- // 加入更新條件
- strSQL.Append(condition);
- return strSQL;
- }
- public class BaseSet
- {
- public static string NULL
- {
- get { return "@null"; }
- }
- public static string DateTimeShortNull
- {
- get { return "0001-1-1 0:00:00"; }
- }
- public static string DateTimeLongNull
- {
- get { return "0001-01-01 00:00:00"; }
- }
- public static string PrimaryKey
- {
- get { return "PrimaryKey"; }
- }
- public static string TableName
- {
- get { return "TableName"; }
- }
- }
- #region 實體樣例
- [Serializable]
- public class SortsInfo
- {
- private int _SortID;
- private string _SortName;
- public string TableName
- {
- get { return "Sorts"; }
- }
- public string PrimaryKey
- {
- get { return "SortID"; }
- }
- public int SortID
- {
- get { return _SortID; }
- set
- {
- _SortID = value;
- }
- }
- public string SortName
- {
- get { return _SortName; }
- set
- {
- _SortName = value;
- }
- }
- }
- #endregion