解决办法:将六个字段中的内容用sql语句实现合并,将其作为一个新的字段显示出来,具体的实现请看代码:
代码如下:
#region 根据动态生成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,不包括难度等级约束
/// <summary>
/// 根据动态生成的数据库表名,从该表中选出QuestionId,ChapterId,QuestionTypeId,Point,
/// Degree,Fraction,QuestioinContent,IsValid等内容,不包括难度等级约束
/// </summary>
/// <param name="strDataTableName"></param>
/// <returns></returns>
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 as nvarchar(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 = sqlHelper.ExecuteQuery(strsql,paras,CommandType.Text);
return dt;
}
catch
{
throw new Exception("从动态生成的数据库表中获取QuestionId,ChapterId,QuestionTypeId,Point失败(不包括难度等级)");
}
finally
{
sqlHelper.Close();
}
}
#endregion
其中使用cast函数的strSql语句所起到的作用就是将多个字段合并成一个新字段。另外需要注意的是strSql语句中的 “ + ” 号,如果需要合并的字段的内容是Text类型的,是不支持该符号的,这时我们需要将其转换成nvarchar类型。到此多列合并问题完美解决。声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com