Aspx code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" DEBUG = "true" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> default. aspx </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "font-size: 12px; text-align: center;"> content of a table in the database (for details about the loop concept, see) </div>
<Asp: Label id = "lblcomment" runat = "server"> </ASP: Label>
</Form>
</Body>
</Html>
CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// WebService Ws = new WebService ();
Lblcomment. Text = gettablecontent (". // sqlexpress", "video", "sa", "111", "v_rate ");
}
/// <Summary>
/// View the content of a table (five parameters)
/// </Summary>
/// <Param name = "host"> database server name </param>
/// <Param name = "Database"> database name </param>
/// <Param name = "uid"> Database User Name </param>
/// <Param name = "PWD"> Database User Password </param>
/// <Param name = "table"> name of the table in the database </param>
/// <Returns> </returns>
Public String gettablecontent (string host, string database, string uid, string PWD, string table)
{
String cnstr = "Server =" + host + "; database =" + database + "; uid =" + uid + "; Pwd =" + PWD + ";";
Sqlconnection conn = new sqlconnection (cnstr );
Sqldataadapter cmd = new sqldataadapter ("select * from" + Table, Conn );
Dataset DS = new dataset ();
Cmd. Fill (DS, "table ");
Datatable DT;
Dt = Ds. Tables ["table"];
String rsstring;
Rsstring = "<Table width =/" 100%/"border =/" 0/"bgcolor =/" #333333/"cellpadding =/" 1/"cellspacing =/" 1/ "Style =/" font-size: 12px;/">"
+ "<Tr bgcolor =/" White/"> ";
For (INT I = 0; I <= DT. Columns. Count-1; I ++)
{
Rsstring + = "<TD>" + dt. Columns [I]. columnname + "</TD> ";
}
Rsstring + = "</tr> ";
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Rsstring + = "<tr bgcolor =/" White/"> ";
For (Int J = 0; j <= DT. Columns. Count-1; j ++)
{
Rsstring + = "<TD>" + dt. Rows [I] [J] + "</TD> ";
}
Rsstring + = "</tr> ";
}
Rsstring + = "</table> ";
Return rsstring;
}
}