时间:2021-07-01 10:21:17 帮助过:12人阅读
#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类型。到此多列合并问题完美解决。