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