SoFunction
Updated on 2025-04-08

Learn from scratch - Basics Page 3/7

private void Page_Load(object sender, e)
{
StringBuilder sbTable = new StringBuilder(); // Statement for outputting tables

string strConnection = "Provider=.4.0;Data Source="
+ (["Database 1"]);

// Statement for connecting to the database
OleDbConnection conn = new OleDbConnection(strConnection);
// Create a DbCommand object
OleDbCommand cmd = ();
= "SELECT * FROM Book";

// Open the database
();

// Use DataReader to read data
OleDbDataReader dr = ();

("<table cellSpacing="0" cellPadding="0" border="1"><tr>");
("<td>Title of the book</td><td>Author<td><td>Unit Price<td></tr>");
while (())
{
("<tr><td>");
(dr["BookTitle"].ToString());
("</td><td>");
(dr["Author"].ToString());
("</td><td>");
(dr["UnitPrice"].ToString());
("</td><tr>");
}
("</tr></table>");

// Remember that the dr must be shut down after use, otherwise the server will be blocked
();

// DbConnection is hosted and can not be closed
// But for good programming habits, it should be closed
();

(());

}