using System; using System.Data; using System.Data.SqlClient; using System.Collections; using AIMSModel; using AIMSObjectQuery; using System.Collections.Generic; namespace AIMSDAL { internal partial class DOperationRecordInstrumentList { #region 插入实体操作部份 /// /// 插入 /// /// Command对象 /// 实体类对象 /// 标识列值或影响的记录行数 internal static int Insert(SqlCommand cmd, OperationRecordInstrumentList operationRecordInstrumentList) { cmd.Parameters.Clear(); cmd.CommandText = "insert into OperationRecordInstrumentList (OperationRecordId,ApplianceUseType,TagPicture,Remark,JsonTextData,OperatorNo,OperatorName,OperateDate) values (@OperationRecordId,@ApplianceUseType,@TagPicture,@Remark,@JsonTextData,@OperatorNo,@OperatorName,@OperateDate);select @@identity"; //从实体中取出值放入Command的参数列表 cmd.Parameters.Add(new SqlParameter("@OperationRecordId",operationRecordInstrumentList.OperationRecordId.HasValue?(object)operationRecordInstrumentList.OperationRecordId.Value:(object)DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@ApplianceUseType",operationRecordInstrumentList.ApplianceUseType==null?(object)DBNull.Value:(object)operationRecordInstrumentList.ApplianceUseType)); cmd.Parameters.Add(new SqlParameter("@TagPicture",operationRecordInstrumentList.TagPicture==null?(object)DBNull.Value:(object)operationRecordInstrumentList.TagPicture)); cmd.Parameters.Add(new SqlParameter("@Remark",operationRecordInstrumentList.Remark==null?(object)DBNull.Value:(object)operationRecordInstrumentList.Remark)); cmd.Parameters.Add(new SqlParameter("@JsonTextData",operationRecordInstrumentList.JsonTextData==null?(object)DBNull.Value:(object)operationRecordInstrumentList.JsonTextData)); cmd.Parameters.Add(new SqlParameter("@OperatorNo",operationRecordInstrumentList.OperatorNo==null?(object)DBNull.Value:(object)operationRecordInstrumentList.OperatorNo)); cmd.Parameters.Add(new SqlParameter("@OperatorName",operationRecordInstrumentList.OperatorName==null?(object)DBNull.Value:(object)operationRecordInstrumentList.OperatorName)); cmd.Parameters.Add(new SqlParameter("@OperateDate",operationRecordInstrumentList.OperateDate.HasValue?(object)operationRecordInstrumentList.OperateDate.Value:(object)DBNull.Value)); return Convert.ToInt32(cmd.ExecuteScalar()); } /// /// 不使用事务的插入方法 /// /// 实体类对象 /// 标识列值或影响的记录行数 internal static int Insert(OperationRecordInstrumentList operationRecordInstrumentList) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return Insert(cmd, operationRecordInstrumentList); } } } /// /// 使用事务的插入方法 /// /// 实现共享Connection的对象 /// 实体类对象 /// 标识列值或影响的记录行数 internal static int Insert(Connection connection,OperationRecordInstrumentList operationRecordInstrumentList) { return Insert(connection.Command, operationRecordInstrumentList); } #endregion #region 删除实体操作 /// /// 删除 /// /// Command对象 /// 实体类对象 /// 影响的记录行数 internal static int ExcuteDeleteCommand(SqlCommand cmd, OperationRecordInstrumentList operationRecordInstrumentList) { cmd.Parameters.Clear(); cmd.CommandText = "delete from OperationRecordInstrumentList where Id=@Id"; //从实体中取出值放入Command的参数列表 cmd.Parameters.Add(new SqlParameter("@Id", operationRecordInstrumentList.Id)); return cmd.ExecuteNonQuery(); } /// /// 不使用事务的删除方法 /// /// 实体类对象 /// 影响的记录行数 internal static int Delete(OperationRecordInstrumentList operationRecordInstrumentList) { using (SqlConnection conn = new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteDeleteCommand(cmd, operationRecordInstrumentList); } } } /// /// 使用事务的删除方法 /// /// 实现共享Connection的对象 /// 实体类对象 /// 影响的记录行数 internal static int Delete(Connection connection,OperationRecordInstrumentList operationRecordInstrumentList) { return ExcuteDeleteCommand(connection.Command, operationRecordInstrumentList); } /// /// 执行删除命令 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int ExcuteDeleteCommand(SqlCommand cmd, string oql, ParameterList parameters) { //解析过滤部份Sql语句 string filterString = SyntaxAnalyzer.ParseSql(oql, new OperationRecordInstrumentListMap()); if (filterString != string.Empty) { filterString = " where " + filterString; } cmd.Parameters.Clear(); cmd.CommandText = "delete from OperationRecordInstrumentList " + filterString; //添加参数 if (parameters != null) { foreach (string key in parameters.Keys) { cmd.Parameters.Add(new SqlParameter(key, parameters[key])); } } return cmd.ExecuteNonQuery(); } /// /// 不使用事务的删除方法 /// /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int Delete(string oql, ParameterList parameters) { using (SqlConnection conn = new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteDeleteCommand(cmd, oql, parameters); } } } /// /// 使用事务的删除方法 /// /// 实现共享Connection的对象 /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int Delete(Connection connection, string oql, ParameterList parameters) { return ExcuteDeleteCommand(connection.Command, oql, parameters); } #endregion #region 更新实体操作 /// /// 更新 /// /// Command对象 /// 实体类对象 /// 影响的记录行数 internal static int ExcuteUpdateCommand(SqlCommand cmd, OperationRecordInstrumentList operationRecordInstrumentList) { cmd.CommandText = "update OperationRecordInstrumentList set OperationRecordId=@OperationRecordId,ApplianceUseType=@ApplianceUseType,TagPicture=@TagPicture,Remark=@Remark,JsonTextData=@JsonTextData,OperatorNo=@OperatorNo,OperatorName=@OperatorName,OperateDate=@OperateDate where Id=@Id"; //从实体中取出值放入Command的参数列表 cmd.Parameters.Add(new SqlParameter("@OperationRecordId",operationRecordInstrumentList.OperationRecordId.HasValue?(object)operationRecordInstrumentList.OperationRecordId.Value:(object)DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@ApplianceUseType",operationRecordInstrumentList.ApplianceUseType==null?(object)DBNull.Value:(object)operationRecordInstrumentList.ApplianceUseType)); cmd.Parameters.Add(new SqlParameter("@TagPicture",operationRecordInstrumentList.TagPicture==null?(object)DBNull.Value:(object)operationRecordInstrumentList.TagPicture)); cmd.Parameters.Add(new SqlParameter("@Remark",operationRecordInstrumentList.Remark==null?(object)DBNull.Value:(object)operationRecordInstrumentList.Remark)); cmd.Parameters.Add(new SqlParameter("@JsonTextData",operationRecordInstrumentList.JsonTextData==null?(object)DBNull.Value:(object)operationRecordInstrumentList.JsonTextData)); cmd.Parameters.Add(new SqlParameter("@OperatorNo",operationRecordInstrumentList.OperatorNo==null?(object)DBNull.Value:(object)operationRecordInstrumentList.OperatorNo)); cmd.Parameters.Add(new SqlParameter("@OperatorName",operationRecordInstrumentList.OperatorName==null?(object)DBNull.Value:(object)operationRecordInstrumentList.OperatorName)); cmd.Parameters.Add(new SqlParameter("@OperateDate",operationRecordInstrumentList.OperateDate.HasValue?(object)operationRecordInstrumentList.OperateDate.Value:(object)DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@Id", operationRecordInstrumentList.Id)); return cmd.ExecuteNonQuery(); } /// /// 不使用事务的更新方法 /// /// 实体类对象 /// 影响的记录行数 internal static int Update(OperationRecordInstrumentList operationRecordInstrumentList) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteUpdateCommand(cmd, operationRecordInstrumentList); } } } /// /// 使用事务的更新方法 /// /// 实现共享Connection的对象 /// 实体类对象 /// 影响的记录行数 internal static int Update(Connection connection,OperationRecordInstrumentList operationRecordInstrumentList) { return ExcuteUpdateCommand(connection.Command, operationRecordInstrumentList); } /// /// 执行更新命令 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int ExcuteUpdateCommand(SqlCommand cmd, string oql, ParameterList parameters) { //解析过滤部份Sql语句 string updateString = SyntaxAnalyzer.ParseSql(oql, new OperationRecordInstrumentListMap()); cmd.CommandText = "update OperationRecordInstrumentList set " + updateString; cmd.Parameters.Clear(); //添加参数 if (parameters != null) { foreach (string key in parameters.Keys) { cmd.Parameters.Add(new SqlParameter(key, parameters[key])); } } return cmd.ExecuteNonQuery(); } /// /// 不使用事务的更新方法 /// /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int Update(string oql, ParameterList parameters) { using (SqlConnection conn = new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteUpdateCommand(cmd, oql, parameters); } } } /// /// 使用事务的更新方法 /// /// 实现共享Connection的对象 /// 对象查询语句 /// 参数列表 /// 影响的记录行数 internal static int Update(Connection connection, string oql, ParameterList parameters) { return ExcuteUpdateCommand(connection.Command, oql, parameters); } #endregion #region 查询实体集合 /// /// 执行Command获取对象列表 /// /// Command对象 /// 递归类型 /// 递归深度 /// 实体类对象列表 internal static List ExcuteSelectCommand(SqlCommand cmd,RecursiveType recursiveType,int recursiveDepth) { List operationRecordInstrumentListList = new List(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { OperationRecordInstrumentList operationRecordInstrumentList = DataReaderToEntity(dr); operationRecordInstrumentListList.Add(operationRecordInstrumentList); } } return operationRecordInstrumentListList; } /// /// 执行查询命令 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 递归类型 /// 递归深度 /// 实体类对象集合 internal static List ExcuteSelectCommand(SqlCommand cmd, string oql, ParameterList parameters,RecursiveType recursiveType,int recursiveDepth) { //解析过滤部份Sql语句 string filterString = SyntaxAnalyzer.ParseSql(oql, new OperationRecordInstrumentListMap()); if (filterString != string.Empty) { if(filterString.Trim().ToLower().IndexOf("order ")!=0) filterString = " where " + filterString; } cmd.Parameters.Clear(); cmd.CommandText = "select * from OperationRecordInstrumentList " + filterString; //添加参数 if (parameters != null) { foreach (string key in parameters.Keys) { cmd.Parameters.Add(new SqlParameter(key, parameters[key])); } } return ExcuteSelectCommand(cmd, recursiveType, recursiveDepth); } /// /// 根据对象查询语句查询实体集合 /// /// 实体类对象集合 internal static List Select() { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from OperationRecordInstrumentList"; return ExcuteSelectCommand(cmd, RecursiveType.Parent, 1); } } } /// /// 根据对象查询语句查询实体集合 /// /// 递归类型 /// 递归深度 /// 实体类对象集合 internal static List Select(RecursiveType recursiveType, int recursiveDepth) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from OperationRecordInstrumentList"; return ExcuteSelectCommand(cmd, recursiveType, recursiveDepth); } } } /// /// 根据对象查询语句查询实体集合 /// /// 对象查询语句 /// 参数列表 /// 实体类对象集合 internal static List Select(string oql, ParameterList parameters) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteSelectCommand(cmd, oql, parameters, RecursiveType.Parent, 1); } } } /// /// 根据对象查询语句查询实体集合 /// /// 对象查询语句 /// 参数列表 /// 递归类型 /// 递归深度 /// 实体类对象集合 internal static List Select(string oql, ParameterList parameters,RecursiveType recursiveType, int recursiveDepth) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteSelectCommand(cmd, oql, parameters, recursiveType, recursiveDepth); } } } /// /// 根据对象查询语句查询实体集合(启用事务) /// /// 连接对象 /// 对象查询语句 /// 参数列表 /// 递归类型 /// 递归深度 /// 实体类对象集合 internal static List Select(Connection connection, string oql, ParameterList parameters, RecursiveType recursiveType, int recursiveDepth) { return ExcuteSelectCommand(connection.Command, oql, parameters,recursiveType, recursiveDepth); } #endregion #region 查询单个实体 /// /// 递归查询单个实体 /// /// Command对象 /// 递归类型 /// 递归深度 /// 实体对象 internal static OperationRecordInstrumentList ExcuteSelectSingleCommand(SqlCommand cmd,RecursiveType recursiveType,int recursiveDepth) { OperationRecordInstrumentList operationRecordInstrumentList=null; using (SqlDataReader dr = cmd.ExecuteReader()) { if(dr.Read()) operationRecordInstrumentList = DataReaderToEntity(dr); } if(operationRecordInstrumentList==null) return operationRecordInstrumentList; return operationRecordInstrumentList; } /// /// 更据对象查询语句递归查询单个实体 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 递归类型 /// 递归深度 /// 实体对象 internal static OperationRecordInstrumentList ExcuteSelectSingleCommand(SqlCommand cmd, string oql, ParameterList parameters,RecursiveType recursiveType,int recursiveDepth) { //解析过滤部份Sql语句 string filterString = SyntaxAnalyzer.ParseSql(oql, new OperationRecordInstrumentListMap()); if(filterString!=string.Empty) { filterString=" where "+filterString; } cmd.CommandText = "select * from OperationRecordInstrumentList " + filterString; cmd.Parameters.Clear(); //添加参数 if (parameters != null) { foreach (string key in parameters.Keys) { cmd.Parameters.Add(new SqlParameter(key, parameters[key])); } } return ExcuteSelectSingleCommand(cmd, recursiveType, recursiveDepth); } /// /// 更据对象查询语句递归查询单个实体 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 递归类型 /// 递归深度 /// 实体对象 internal static OperationRecordInstrumentList SelectSingle(string oql, ParameterList parameters, RecursiveType recursiveType, int recursiveDepth) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return ExcuteSelectSingleCommand(cmd, oql, parameters, recursiveType, recursiveDepth); } } } /// /// 更据对象查询语句查询单个实体 /// /// Command对象 /// 对象查询语句 /// 参数列表 /// 实体对象 internal static OperationRecordInstrumentList SelectSingle(string oql, ParameterList parameters) { return SelectSingle(oql,parameters,RecursiveType.Parent,1); } /// /// 更据对象查询语句并启用事务查询单个实体 /// /// 连接对象 /// 对象查询语句 /// 参数列表 /// 实体对象 internal static OperationRecordInstrumentList SelectSingle(Connection connection, string oql, ParameterList parameters, RecursiveType recursiveType, int recursiveDepth) { return ExcuteSelectSingleCommand(connection.Command, oql, parameters, recursiveType, recursiveDepth); } /// /// 更据主键值递归查询单个实体 /// /// Command对象 /// 主键值 /// 递归类型 /// 递归深度 /// 实体对象 internal static OperationRecordInstrumentList SelectSingle(SqlCommand cmd, int? id,RecursiveType recursiveType,int recursiveDepth) { cmd.Parameters.Clear(); if(id.HasValue) { cmd.CommandText = "select * from OperationRecordInstrumentList where Id=@pk"; cmd.Parameters.Add(new SqlParameter("@pk",id.Value)); } else { cmd.CommandText = "select * from OperationRecordInstrumentList where Id is null"; } return ExcuteSelectSingleCommand(cmd, recursiveType, recursiveDepth); } /// /// 按主键字段查询特定实体 /// /// 主键值 /// 实体类对象 internal static OperationRecordInstrumentList SelectSingle(int? id) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return SelectSingle(cmd,id,RecursiveType.Parent,1); } } } /// /// 按主键字段查询特定实体 /// /// 主键值 /// 递归类型 /// 递归深度 /// 实体类对象 internal static OperationRecordInstrumentList SelectSingle(int? id, RecursiveType recursiveType, int recursiveDepth) { using(SqlConnection conn=new SqlConnection(Connection.ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { return SelectSingle(cmd,id, recursiveType, recursiveDepth); } } } /// /// 使用事务并按主键字段查询特定实体 /// /// 连接对象 /// 主键值 /// 实体类对象 internal static OperationRecordInstrumentList SelectSingle(Connection connection,int? id, RecursiveType recursiveType, int recursiveDepth) { return SelectSingle(connection.Command, id, recursiveType, recursiveDepth); } #endregion /// /// 从DataReader中取出值生成实体对象 /// /// 查询对象 /// 过滤条件字符串 private static OperationRecordInstrumentList DataReaderToEntity(SqlDataReader dr) { OperationRecordInstrumentList entity = new OperationRecordInstrumentList (); if(dr["Id"]!=System.DBNull.Value) { entity.Id=Convert.ToInt32(dr["Id"]); } if(dr["OperationRecordId"]!=System.DBNull.Value) { entity.OperationRecordId=Convert.ToInt32(dr["OperationRecordId"]); } if(dr["ApplianceUseType"]!=System.DBNull.Value) { entity.ApplianceUseType=dr["ApplianceUseType"].ToString(); } if(dr["TagPicture"]!=System.DBNull.Value) { entity.TagPicture=dr["TagPicture"].ToString(); } if(dr["Remark"]!=System.DBNull.Value) { entity.Remark=dr["Remark"].ToString(); } if(dr["JsonTextData"]!=System.DBNull.Value) { entity.JsonTextData=dr["JsonTextData"].ToString(); } if(dr["OperatorNo"]!=System.DBNull.Value) { entity.OperatorNo=dr["OperatorNo"].ToString(); } if(dr["OperatorName"]!=System.DBNull.Value) { entity.OperatorName=dr["OperatorName"].ToString(); } if(dr["OperateDate"]!=System.DBNull.Value) { entity.OperateDate=Convert.ToDateTime(dr["OperateDate"]); } return entity; } } }