private void AddParam(DbCommand dbCommand, ORM_FieldAttribute ORM_FieldAttribute, string sourceColumn) { DbParameter dbp = dbCommand.CreateParameter(); dbp.ParameterName = "@" + sourceColumn; dbp.DbType = ORM_FieldAttribute.Type; dbp.Size = ORM_FieldAttribute.Size; dbp.SourceColumn = sourceColumn; dbCommand.Parameters.Add(dbp); }
internal void GenerateSQL(Type ORM_Model, DataTable dt, DbProviderType dbProviderType) { object[] attrClass; //查找ORM的属性定义 ORM_TableAttribute classAttribute = null; attrClass = ORM_Model.GetCustomAttributes(typeof(ORM_TableAttribute), false); if (attrClass != null && attrClass.Length == 0) { throw new Exception("ORM_ObjectClassAttribute未定义!"); } classAttribute = attrClass[0] as ORM_TableAttribute; //查找ORM控制并发的属性定义 ORM_ConcurrentAttribute concurrentAttribute = null; attrClass = ORM_Model.GetCustomAttributes(typeof(ORM_ConcurrentAttribute), false); if (attrClass != null && attrClass.Length > 0 && classAttribute.IsSummaryTable)//仅主表控制并发 { concurrentAttribute = attrClass[0] as ORM_ConcurrentAttribute; _ConcurrentAttribute = concurrentAttribute; } //是否主表属性 _IsSummary = classAttribute.IsSummaryTable; _PrimaryFieldName = classAttribute.PrimaryKey;//主键,复合主键盘用";"分开 //生成一个新的对象 object obj = ORM_Model.Assembly.CreateInstance(ORM_Model.FullName); FieldInfo[] infos = GetAllInfos(ORM_Model); ArrayList fieldArr = new ArrayList(); _cmdInsert = ProviderFactory.Instance.GetDbProviderFactory(dbProviderType).CreateCommand(); _cmdUpdate = ProviderFactory.Instance.GetDbProviderFactory(dbProviderType).CreateCommand(); _cmdDelete = ProviderFactory.Instance.GetDbProviderFactory(dbProviderType).CreateCommand(); _cmdInsert.CommandType = CommandType.Text; _cmdUpdate.CommandType = CommandType.Text; _cmdDelete.CommandType = CommandType.Text; foreach (FieldInfo info in infos) { //取字段属性定义 object[] attrField = info.GetCustomAttributes(typeof(ORM_FieldAttribute), false); if (attrField == null || attrField.Length == 0) { continue; } string fieldName = info.GetValue(obj).ToString(); ORM_FieldAttribute fieldAttr = attrField[0] as ORM_FieldAttribute; //用作构造sql语句中的参数 if (fieldAttr.IsAddOrUpdate) { //取得最多的参数,与生成的sql语句作匹配,构造SQL参数语句 if (!_cmdInsert.Parameters.Contains("@" + fieldName)) { this.AddParam(_cmdInsert, fieldAttr, fieldName); //_cmdInsert.Parameters.Add("@" + fieldName, fieldAttr.Type, fieldAttr.Size, fieldName); } if (!_cmdUpdate.Parameters.Contains("@" + fieldName)) { this.AddParam(_cmdUpdate, fieldAttr, fieldName); //_cmdUpdate.Parameters.Add("@" + fieldName, fieldAttr.Type, fieldAttr.Size, fieldName); } ColumnProperty colProper = new ColumnProperty(fieldName, fieldAttr.IsPrimaryKey ? true : false); fieldArr.Add(colProper); } //生成主键的参数 if (fieldAttr.IsPrimaryKey) { if (!_cmdUpdate.Parameters.Contains("@" + fieldName)) { this.AddParam(_cmdUpdate, fieldAttr, fieldName); //_cmdUpdate.Parameters.Add("@" + fieldName, fieldAttr.Type, fieldAttr.Size, fieldName); } if (!_cmdDelete.Parameters.Contains("@" + fieldName)) { this.AddParam(_cmdDelete, fieldAttr, fieldName); //_cmdDelete.Parameters.Add("@" + fieldName, fieldAttr.Type, fieldAttr.Size, fieldName); } } //控制并发的字段名 if (concurrentAttribute != null && concurrentAttribute.TimestampFieldName == fieldName) { if (!_cmdUpdate.Parameters.Contains("@" + fieldName)) { this.AddParam(_cmdUpdate, fieldAttr, fieldName); } // _cmdUpdate.Parameters.Add("@" + fieldName, fieldAttr.Type, fieldAttr.Size, fieldName); } //流水号字段名 if (fieldAttr.IsDocFieldName) { _DocNoFieldName = fieldName; } //外键字段名 if (fieldAttr.IsForeignKey) { _ForeignFieldName = _ForeignFieldName + ";" + fieldName; //组合外键 } } _addOrUpdateCol = fieldArr; _sqlInsert = GernerateInsertSql(classAttribute.TableName, classAttribute.PrimaryKey, fieldArr, dbProviderType); //新增的SQL _sqlUpdate = GernerateUpdateSql(classAttribute.TableName, classAttribute.PrimaryKey, fieldArr, dbProviderType); //修改的SQL _sqlDelete = GernerateDeleteSql(classAttribute.TableName, classAttribute.PrimaryKey, fieldArr, dbProviderType); //删除的SQL }