2023年6月21日发(作者:)

DataTable多列合并问题轻松搞定:

在做考试系统⼿动⽣成试卷部分时由于题库的表结构不相同,导致同样的Gridview(已模板化后的,其结构已固定)在显⽰时不能同时两种不同结构的数据。如GridView结构如下所⽰:

这种固定的格式显⽰的是以选择题为代表的数据结构,但是因为选择题题库表结构与论述题题库表结构不相同,所以⽆法直接显⽰以论述题为代表的数据结构。这时如何在这个固定的GridView中显⽰不同的数据呢?其实在仔细观察后我们可以发现他们唯⼀的区别在于“答案”这列的数据不同,在选择题类型中,该字段的值仅为⼀个选项⽽已,但是对于论述题等类型,其问题有六个,对应的答案也应该有六列才对。分析到此,可以总结⼀下,最终要解决的问题是如何将六列的答案显⽰在⼀列。:将六个字段中的内容⽤sql语句实现合并,将其作为⼀个新的字段显⽰出来,具体的实现请看代码:

复制代码 代码如下:#region 根据动态⽣成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,不包括难度等级约束

///

/// 根据动态⽣成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,

/// Degree,Fraction,QuestioinContent,IsValid等内容,不包括难度等级约束

///

///

///

public DataTable BindQuestion(string strTableName,string strChapterName,string strQuestionTypeName)

{

try

{

DataTable dt = new DataTable ();

if (strQuestionTypeName != "论述题" && strQuestionTypeName != "案例分析题")

{

strsql = "select * from " + strTableName + " where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

}

else

{

strsql = "select QuestionId,ChapterId,QuestionTypeId,Point,Degree,Fraction,QuestionContent,cast(Answer1 asnvarchar(4000)) + cast(Answer2 as nvarchar(4000)) + cast(Answer3 as nvarchar(4000)) + cast(Answer4 as nvarchar(4000))+ cast(Answer5 as nvarchar(4000)) + cast(Answer6 as nvarchar(4000)) AS CorrectAnswer,IsValid from " + strTableName + "where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

}

//strsql = "select * from " + strTableName + " where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

SqlParameter[] paras = new SqlParameter[]{

new SqlParameter("@chapterid",strChapterName),

new SqlParameter("@questiontypeid",strQuestionTypeName)

};

dt = eQuery(strsql,paras,);

return dt;

}

catch

{

throw new Exception("从动态⽣成的数据库表中获取QuestionId,ChapterId,QuestionTypeId,Point失败(不包括难度等级)");

}

finally

{

();

}

}

#endregion

其中使⽤cast函数的strSql语句所起到的作⽤就是将多个字段合并成⼀个新字段。另外需要注意的是strSql语句中的 “ + ” 号,如果需要合并的字段的内容是Text类型的,是不⽀持该符号的,这时我们需要将其转换成nvarchar类型。到此多列合并问题完美解决。

2023年6月21日发(作者:)

DataTable多列合并问题轻松搞定:

在做考试系统⼿动⽣成试卷部分时由于题库的表结构不相同,导致同样的Gridview(已模板化后的,其结构已固定)在显⽰时不能同时两种不同结构的数据。如GridView结构如下所⽰:

这种固定的格式显⽰的是以选择题为代表的数据结构,但是因为选择题题库表结构与论述题题库表结构不相同,所以⽆法直接显⽰以论述题为代表的数据结构。这时如何在这个固定的GridView中显⽰不同的数据呢?其实在仔细观察后我们可以发现他们唯⼀的区别在于“答案”这列的数据不同,在选择题类型中,该字段的值仅为⼀个选项⽽已,但是对于论述题等类型,其问题有六个,对应的答案也应该有六列才对。分析到此,可以总结⼀下,最终要解决的问题是如何将六列的答案显⽰在⼀列。:将六个字段中的内容⽤sql语句实现合并,将其作为⼀个新的字段显⽰出来,具体的实现请看代码:

复制代码 代码如下:#region 根据动态⽣成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,不包括难度等级约束

///

/// 根据动态⽣成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,

/// Degree,Fraction,QuestioinContent,IsValid等内容,不包括难度等级约束

///

///

///

public DataTable BindQuestion(string strTableName,string strChapterName,string strQuestionTypeName)

{

try

{

DataTable dt = new DataTable ();

if (strQuestionTypeName != "论述题" && strQuestionTypeName != "案例分析题")

{

strsql = "select * from " + strTableName + " where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

}

else

{

strsql = "select QuestionId,ChapterId,QuestionTypeId,Point,Degree,Fraction,QuestionContent,cast(Answer1 asnvarchar(4000)) + cast(Answer2 as nvarchar(4000)) + cast(Answer3 as nvarchar(4000)) + cast(Answer4 as nvarchar(4000))+ cast(Answer5 as nvarchar(4000)) + cast(Answer6 as nvarchar(4000)) AS CorrectAnswer,IsValid from " + strTableName + "where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

}

//strsql = "select * from " + strTableName + " where ChapterId=@chapterid and QuestionTypeId=@questiontypeid";

SqlParameter[] paras = new SqlParameter[]{

new SqlParameter("@chapterid",strChapterName),

new SqlParameter("@questiontypeid",strQuestionTypeName)

};

dt = eQuery(strsql,paras,);

return dt;

}

catch

{

throw new Exception("从动态⽣成的数据库表中获取QuestionId,ChapterId,QuestionTypeId,Point失败(不包括难度等级)");

}

finally

{

();

}

}

#endregion

其中使⽤cast函数的strSql语句所起到的作⽤就是将多个字段合并成⼀个新字段。另外需要注意的是strSql语句中的 “ + ” 号,如果需要合并的字段的内容是Text类型的,是不⽀持该符号的,这时我们需要将其转换成nvarchar类型。到此多列合并问题完美解决。