功能:從資料表中取出資料放入Dataset的DataTable中,再從DataTable抓取資料並顯示。
public partial class Default13 : System.Web.UI.Page
{
string myString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DBInit();
}
}
protected void DBInit()
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = null;
try
{
myAdapter = new SqlDataAdapter("Select * From test", Conn);
DataSet ds = new DataSet();
//將dataset中的資料,放入名為test的datatable中
myAdapter.Fill(ds, "test");
DataTable myTable = ds.Tables["test"];
string myString;
myString = "<table border=1><tr><td>id</td><td>test_time</td><td>title</td><td>author</td></tr>";
for (int i = 0; i < myTable.Rows.Count; i++)
{ //---- 把DataTable裡面的紀錄,一列一列(Row)地呈現 ----
myString = myString + "<tr>";
myString = myString + "<td>" + myTable.Rows[i]["id"] + "</td>";
myString = myString + "<td>" + myTable.Rows[i]["test_time"] + "</td>";
myString = myString + "<td>" + myTable.Rows[i]["title"] + "</td>";
myString = myString + "<td>" + myTable.Rows[i]["author"] + "</td>";
myString = myString + "</tr>";
}
myString = myString + "</table>";
Label1.Text = myString;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
留言列表