/// <summary> /// 设置一个命令 /// </summary> /// <param name="ob"></param> /// <param name="command"></param> /// <param name="TagetType"></param> /// <param name="cxt"></param> public void SetValues(object ob, System.Data.SqlClient.SqlCommand command, Type TagetType, String ParamPre = "", bool GernerateSerialNo = false, IDBContext cxt = null) { var Propertitest = TagetType.GetProperties(); var factory = new MSSQLScriptSqlPatternFactory(); foreach (var property in Propertitest) { foreach (var colattr in helper.GetColNameAttributes(property)) { if (colattr.NoMap) { continue; } var paramName = "@" + ParamPre + helper.GetColName(colattr, property); object propertyob = new ORMHelper().GetDbObject(property, ob, colattr.PropertyName) ?? ""; if (command.Parameters.Contains(paramName)) { if (GernerateSerialNo && cxt != null && String.IsNullOrEmpty( //(command.Parameters[paramName].Value ?? "" (new ORMHelper().GetDbObject(property, ob, colattr.PropertyName) ?? "" ).ToString() ) == true ) { var auto = property.GetCustomAttributes(typeof(SerialNoObject), true); if (auto.Length > 0) { var seriSetting = auto[0] as SerialNoObject; command.Parameters[paramName].Value = cxt.GetSerialNo(seriSetting.SerialPre, seriSetting.Len, seriSetting.DateFormateStr, ""); property.SetValue(ob, command.Parameters[paramName].Value, null); } else { command.Parameters[paramName].Value = propertyob; } //else if(String.IsNullOrEmpty( (command.Parameters[paramName].Value ?? "").ToString())==false) //command.Parameters[paramName].Value = new ORMHelper().GetDbObject(property, ob, colattr.PropertyName) ?? ""; } else { var insertDb = new ORMHelper().GetDbObject(property, ob, colattr.PropertyName); if (insertDb is DateTime && insertDb != null && String.IsNullOrEmpty(colattr.FormatStr) == false) { insertDb = ((DateTime)insertDb).ToString(colattr.FormatStr); } command.Parameters[paramName].Value = insertDb ?? ""; } } } } }
/// <summary> /// /// </summary> /// <param name="ob"></param> /// <param name="cxt"></param> /// <param name="TargetType"></param> /// <returns></returns> /// <remarks> /// 2013.8.7 /// 将MSSQLScriptSqlPatternFactory改为可以得到列信息和名称信息 /// </remarks> private System.Data.IDbCommand GenerateCreateSql(object ob, IDBContext cxt, Type TargetType) { System.Data.SqlClient.SqlCommand command = null; var patternFactory = new MSSQLScriptSqlPatternFactory(); if (insertCommand.ContainsKey(TargetType) == false) { //得到表名 command = new System.Data.SqlClient.SqlCommand(); String TableName = helper.GetTable(TargetType); var Properties = TargetType.GetProperties(); String Cols = ""; String Values = ""; for (int i = 0; i < Properties.Length; i++) { if (Properties[i].PropertyType.IsArray) { continue; } var attribute = helper.GetColNameAttributes(Properties[i]); String paramName = "@" + Properties[i].Name; string ColName = ""; if (attribute.Count > 0) { for (int k = 0; k < attribute.Count; k++) { var Col = attribute[k]; if (Col.NoMap == true) { continue; } else if (Col.IsAutoGenerate == GenerationType.OnInSert) { var auto = Properties[i].GetCustomAttributes(typeof(SerialNoObject), true); if (auto.Length > 0) { var seriSetting = auto[0] as SerialNoObject; Properties[i].SetValue(ob, cxt.GetSerialNo(seriSetting.SerialPre, seriSetting.Len, seriSetting.DateFormateStr, ""), null); } else { continue; } } ColName = helper.GetColName(Col, Properties[i]); paramName = "@" + ColName; var valueObject = new ORMHelper().GetDbObject(Properties[i], ob, Col.PropertyName); if (valueObject is DateTime && valueObject != null && String.IsNullOrEmpty(Col.FormatStr) == false) { valueObject = ((DateTime)valueObject).ToString(Col.FormatStr); } Cols += "[" + ColName + "],"; Values += paramName + ","; command.Parameters.Add(new System.Data.SqlClient.SqlParameter(paramName, valueObject ?? "")); } } } command.CommandText = "INSERT " + TableName + "(" + Cols.Substring(0, Cols.Length - 1) + ") VALUES(" + Values.Substring(0, Values.Length - 1) + ")"; insertCommand.Add(TargetType, command); //return command; } command = (insertCommand[TargetType] as System.Data.SqlClient.SqlCommand).Clone(); SetValues(ob, command, TargetType, "", true, cxt); return(command); }