VS. NET (C #) Database Interface C # DataSet object access demonstration (code case), vs.net
Based on the above section, output the data of the two tables created in the DataSet object to the page. Achieve the following results:
Click "Access DataSet object" to display all data tables in DataSet.
Add ShowDataSet (DataSet ds) function call by clicking an event on the button
protected void Button1_Click(object sender, EventArgs e) { ShowDataSet(ds_Forum); }
Private void ShowDataSet (DataSet ds) {// construct the output string StringBuilder htmlStr = new System. text. stringBuilder (); // traverses all tables in Forum (use foreach traversal) foreach (DataTable dt in ds. tables) {// The table starts htmlStr. append ("<table border = '1' cellPadding = '5' cellSpacing = '0' style = 'font-size: 9pt; font: '> "); # the header of the region table // the header starts htmlStr. append ("<tr style = 'background-color = # f0f0f0 '>"); // traverses all columns in the table (use for traversal) for (int I = 0; I <dt. Columns. count; I ++) {htmlStr. append (string. format ("<td> <strong> {0} </strong> </td>", dt. columns [I]. columnName); // header} htmlStr. append ("</tr>"); // end of a row in the header # endregion # data row in the region table // traverse all rows in the table for (int I = 0; I <dt. rows. count; I ++) {// The data row starts htmlStr. append ("<tr>"); // traverses the column for (int j = 0; j <dt. columns. count; j ++) {if (! Dt. rows [I]. isNull (j) {htmlStr. append (string. format ("<td> {0} </td>", dt. rows [I] [j]);} else {htmlStr. append (string. format ("<td> {0} </td>", string. empty);} // end of the Data row htmlStr. append ("</tr>") ;}# endregion // the end of the table htmlStr. append ("</table> <br>");} // output DataSet page data Response. write (htmlStr );}