📄 dclasstempcourse.cs
字号:
#region 查询实体集合
/// <summary>
/// 根据查询对象构建过滤条件查询
/// </summary>
/// <param name="searcher">查询对象</param>
/// <returns>实体类对象列表</returns>
public ArrayList Select(ClassTempCourseSearcher searcher)
{
ArrayList entities = new ArrayList();
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
entities = Select(cmd,searcher);
}
}
return entities;
}
/// <summary>
/// 使用事务并且依据查询对象构建过滤条件查询
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="searcher">查询对象</param>
/// <returns>实体类对象列表</returns>
public ArrayList Select(IConnection connection, ClassTempCourseSearcher searcher)
{
SqlCommand cmd=connection.Command as SqlCommand;
return Select(cmd,searcher);
}
public ArrayList Select(SqlCommand command,ClassTempCourseSearcher searcher)
{
string condition=string.Empty;
command.Parameters.Clear();
ArrayList entities = new ArrayList();
if(searcher.ClassTempCourseIDIsValid)
{
condition += " and Class_TempCourse_ID=@Class_TempCourse_ID_C";
command.Parameters.Add(new SqlParameter("@Class_TempCourse_ID_C",searcher.ClassTempCourseID));
}
if(searcher.ClassIDIsValid)
{
condition += " and Class_ID=@Class_ID_C";
command.Parameters.Add(new SqlParameter("@Class_ID_C",searcher.ClassID));
}
if(searcher.TempCourseIDIsValid)
{
condition += " and TempCourse_ID=@TempCourse_ID_C";
command.Parameters.Add(new SqlParameter("@TempCourse_ID_C",searcher.TempCourseID));
}
if(searcher.TempCourseDateIsValid)
{
condition += " and TempCourse_Date=@TempCourse_Date_C";
command.Parameters.Add(new SqlParameter("@TempCourse_Date_C",searcher.TempCourseDate));
}
if(searcher.IsAmIsValid)
{
condition += " and IsAm=@IsAm_C";
command.Parameters.Add(new SqlParameter("@IsAm_C",searcher.IsAm));
}
if(searcher.LessonHourIsValid)
{
condition += " and Lesson_Hour=@Lesson_Hour_C";
command.Parameters.Add(new SqlParameter("@Lesson_Hour_C",searcher.LessonHour));
}
if(searcher.IsFrontIsValid)
{
condition += " and Is_Front=@Is_Front_C";
command.Parameters.Add(new SqlParameter("@Is_Front_C",searcher.IsFront));
}
if(searcher.TeacherIDIsValid)
{
condition += " and Teacher_ID=@Teacher_ID_C";
command.Parameters.Add(new SqlParameter("@Teacher_ID_C",searcher.TeacherID));
}
if(condition != string.Empty)
{
condition=" where"+condition.Substring(4);
}
command.CommandText = "select * from ClassTempCourse"+condition;
return ExcuteSelectCommand(command);
}
/// <summary>
/// 执行Command获取对象列表
/// </summary>
/// <param name="cmd">Command对象</param>
/// <returns>实体类对象列表</returns>
private ArrayList ExcuteSelectCommand(SqlCommand cmd)
{
ArrayList entities = new ArrayList();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
ClassTempCourse entity = DataReaderToEntity(dr);
entities.Add(entity);
}
}
SetForeignKeyEntity(cmd, ref entities);
return entities;
}
private void SetForeignKeyEntity(SqlCommand command, ref ArrayList entities)
{
//由外键获取相关实体
DTempCourse dTempCourse=new DTempCourse();
DClass dClass=new DClass();
DEmployee dEmployee=new DEmployee();
foreach(ClassTempCourse entity in entities)
{
entity.TempCourse = dTempCourse.SelectSingle(command,entity.TempCourseID);
entity.Class = dClass.SelectSingle(command,entity.ClassID);
entity.Employee = dEmployee.SelectSingle(command,entity.TeacherID);
}
}
/// <summary>
/// 查询所有实体
/// </summary>
/// <returns>实体类对象列表</returns>
public ArrayList Select()
{
ArrayList entities = new ArrayList();
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from ClassTempCourse";
entities=ExcuteSelectCommand(cmd);
}
return entities;
}
}
/// <summary>
/// 使用事务查询查询所有实体
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="searcher">查询对象</param>
/// <returns>实体类对象列表</returns>
public ArrayList Select(IConnection connection)
{
ArrayList entities = new ArrayList();
SqlCommand cmd=connection.Command as SqlCommand;
cmd.Parameters.Clear();
cmd.CommandText = "select * from ClassTempCourse";
return ExcuteSelectCommand(cmd);
}
#endregion
#region 查询单个实体
/// <summary>
/// 按主键字段查询特定实体
/// </summary>
/// <param name="pkValue">主键值</param>
/// <returns>实体类对象</returns>
public ClassTempCourse SelectSingle(object pkValue)
{
ClassTempCourse entity=null;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
entity=SelectSingle(cmd,pkValue);
}
}
return entity;
}
/// <summary>
/// 使用事务并按主键字段查询特定实体
/// </summary>
/// <param name="pkValue">主键值</param>
/// <returns>实体类对象</returns>
public ClassTempCourse SelectSingle(IConnection connection,object pkValue)
{
SqlCommand cmd=connection.Command as SqlCommand;
return SelectSingle(cmd,pkValue);
}
public ClassTempCourse SelectSingle(SqlCommand command,object pkValue)
{
ClassTempCourse entity=null;
command.Parameters.Clear();
command.CommandText = "select * from ClassTempCourse where Class_TempCourse_ID=@pk";
command.Parameters.Add(new SqlParameter("@pk",pkValue));
using (SqlDataReader dr = command.ExecuteReader())
{
if(dr.Read())
entity = DataReaderToEntity(dr);
}
//由外键获取相关实体
DTempCourse dTempCourse=new DTempCourse();
DClass dClass=new DClass();
DEmployee dEmployee=new DEmployee();
entity.TempCourse = dTempCourse.SelectSingle(command,entity.TempCourseID);
entity.Class = dClass.SelectSingle(command,entity.ClassID);
entity.Employee = dEmployee.SelectSingle(command,entity.TeacherID);
return entity;
}
#endregion
/// <summary>
/// 从DataReader中取出值生成实体对象
/// </summary>
/// <param name="searcher">查询对象</param>
/// <returns>过滤条件字符串</returns>
private ClassTempCourse DataReaderToEntity(SqlDataReader dr)
{
ClassTempCourse entity = new ClassTempCourse ();
if(dr["Class_TempCourse_ID"]!=System.DBNull.Value)
{
entity.ClassTempCourseID=Convert.ToInt32(dr["Class_TempCourse_ID"]);
}
if(dr["Class_ID"]!=System.DBNull.Value)
{
entity.ClassID=Convert.ToInt32(dr["Class_ID"]);
}
if(dr["TempCourse_ID"]!=System.DBNull.Value)
{
entity.TempCourseID=Convert.ToInt32(dr["TempCourse_ID"]);
}
if(dr["TempCourse_Date"]!=System.DBNull.Value)
{
entity.TempCourseDate=Convert.ToDateTime(dr["TempCourse_Date"]);
}
if(dr["IsAm"]!=System.DBNull.Value)
{
entity.IsAm=Convert.ToBoolean(dr["IsAm"]);
}
if(dr["Lesson_Hour"]!=System.DBNull.Value)
{
entity.LessonHour=Convert.ToInt32(dr["Lesson_Hour"]);
}
if(dr["Is_Front"]!=System.DBNull.Value)
{
entity.IsFront=Convert.ToBoolean(dr["Is_Front"]);
}
if(dr["Teacher_ID"]!=System.DBNull.Value)
{
entity.TeacherID=Convert.ToInt32(dr["Teacher_ID"]);
}
return entity;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -