In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you DataTable how to solve the problem of multi-column merger, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Background of the question:
When the examination system generates the test paper manually, because the table structure of the item bank is different, the same Gridview (whose structure has been fixed after being templated) can not be displayed with two different structures of data at the same time. The structure of the GridView is as follows:
This fixed format shows the data structure represented by multiple choice questions, but because the structure of the selected item bank table is different from that of the discussion item bank table, it is impossible to directly display the data structure represented by the discussion title. How do you display different data in this fixed GridView? In fact, after careful observation, we can find that the only difference between them is the data in the column of "answer". In the type of multiple choice questions, the value of this field is only one option, but for types such as discussion questions, there are six questions. the corresponding answer should also have six columns. At this point, we can sum up that the final problem to be solved is how to display the answers of the six columns in one column.
Solution: merge the contents of the six fields with SQL statements and display them as a new field. See the code for the specific implementation:
The copy code is as follows:
# region selects QuestionId,ChapterId,QuestionTypeId,Point from the dynamically generated database table name, excluding the difficulty level constraint
/ / /
/ / QuestionId,ChapterId,QuestionTypeId,Point is selected from the dynamically generated database table name
/ / Degree,Fraction,QuestioinContent,IsValid and other contents, excluding difficulty level constraints
/ / /
/ / /
/ / /
Public DataTable BindQuestion (string strTableName,string strChapterName,string strQuestionTypeName)
{
Try
{
DataTable dt = new DataTable ()
If (strQuestionTypeName! = "discussion questions" & & strQuestionTypeName! = "case Analysis questions")
{
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 ("failed to get QuestionId,ChapterId,QuestionTypeId,Point from dynamically generated database tables (excluding difficulty level)")
}
Finally
{
SqlHelper.Close ()
}
}
# endregion
The role of the strSql statement that uses the cast function is to merge multiple fields into a new field. Another thing to note is the "+" sign in the strSql statement. If the content of the field that needs to be merged is of type Text, the symbol is not supported, so we need to convert it to type nvarchar. So far, the problem of multi-column merger has been solved perfectly.
These are all the contents of the article "how to solve the problem of multi-column merging in DataTable". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.