Using System.Data; Using System.Data.OleDb;
<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="C#" Debug="true" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { BindTable(); } //*** Bind To Table ***// void BindTable() { //*** Test DataTable Structure ***// //********************************// DataTable dt; int i; dt = CreateDataTable(); //*** Create Table **// Table Tb = new Table(); //Tb.BorderColor = Drawing.Color.Black(); Tb.BorderWidth = 1; //*** Rows ***// TableRow Tr; //*** Column ***// TableCell Tc; //*** Create Header ***// //*** New Rows ***// Tr = new TableRow(); //*** New Cell (Header CustomerID) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadCustomerID = new Label(); lblHeadCustomerID.Text = "CustomerID"; lblHeadCustomerID.Font.Bold = true; Tc.Controls.Add(lblHeadCustomerID); //*** Add lblHeadCustomerID To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Header Name) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadName = new Label(); lblHeadName.Text = "Name"; lblHeadName.Font.Bold = true; Tc.Controls.Add(lblHeadName); //*** Add lblHeadName To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Header Email) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadEmail = new Label(); lblHeadEmail.Text = "Email"; lblHeadEmail.Font.Bold = true; Tc.Controls.Add(lblHeadEmail); //*** Add lblHeadEmail To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Header CountryCode) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadCountryCode = new Label(); lblHeadCountryCode.Text = "CountryCode"; lblHeadCountryCode.Font.Bold = true; Tc.Controls.Add(lblHeadCountryCode); //*** Add lblHeadCountryCode To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Header Budget) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadBudget = new Label(); lblHeadBudget.Text = "Budget"; lblHeadBudget.Font.Bold = true; Tc.Controls.Add(lblHeadBudget); //*** Add lblHeadBudget To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Header Used) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblHeadUsed = new Label(); lblHeadUsed.Text = "Used"; lblHeadUsed.Font.Bold = true; Tc.Controls.Add(lblHeadUsed); //*** Add lblHeadUsed To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// Tb.Controls.Add(Tr); //*** End Header ***// //*************** Input DataTable To TableRows ***************// for( i = 0; i<= dt.Rows.Count - 1; i ++) { Tr = new TableRow(); //*** New Cell (CustomerID) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.HorizontalAlign = HorizontalAlign.Center; Tc.BorderWidth = 1; Label lblCustomerID = new Label(); lblCustomerID.Text = dt.Rows[i]["CustomerID"].ToString(); Tc.Controls.Add(lblCustomerID); //*** Add lblCustomerID To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Name) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblName = new Label(); lblName.Text = dt.Rows[i]["Name"].ToString(); Tc.Controls.Add(lblName); //*** Add lblName To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Email) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblEmail = new Label(); lblEmail.Text = dt.Rows[i]["Email"].ToString(); Tc.Controls.Add(lblEmail); //*** Add lblEmail To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (CountryCode) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblCountryCode = new Label(); lblCountryCode.Text = dt.Rows[i]["CountryCode"].ToString(); Tc.Controls.Add(lblCountryCode); //*** Add lblCountryCode To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Budget) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblBudget = new Label(); lblBudget.Text = dt.Rows[i]["Budget"].ToString(); Tc.Controls.Add(lblBudget); //*** Add lblBudget To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// //*** New Cell (Used) ***// Tc = new TableCell(); //Tc.BorderColor = Drawing.Color.Black(); Tc.BorderWidth = 1; Label lblUsed = new Label(); lblUsed.Text = dt.Rows[i]["Used"].ToString(); Tc.Controls.Add(lblUsed); //*** Add lblUsed To Cell ***// Tr.Cells.Add(Tc); //*** Add Cell To Rows ***// Tb.Controls.Add(Tr); } //************ End Input DataTable To TableRows *************// this.lblTable.Controls.Add(Tb); } //*** DataTable ***// protected DataTable CreateDataTable() { System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbDataAdapter dtAdapter = new System.Data.OleDb.OleDbDataAdapter(); DataTable dt = new DataTable(); String strConnString; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb")+";Jet OLEDB:Database Password=;"; objConn = new System.Data.OleDb.OleDbConnection(strConnString); objConn.Open(); String strSQL; strSQL = "SELECT * FROM customer"; dtAdapter = new System.Data.OleDb.OleDbDataAdapter(strSQL, objConn); dtAdapter.Fill(dt); return dt; //*** Return DataTable ***// dtAdapter = null; objConn.Close(); objConn = null; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - System.Data.OleDb</title> </head> <body> <form id="form1" runat="server"> <asp:Label id="lblTable" runat="server"></asp:Label> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท