Tuesday, 4 March 2014

DATA TABLE

<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Data" %>

<script language="C#" runat="server">

private SqlConnectionconn = null;

private SqlCommandcmd = null;

private SqlDataAdapterda = null;

private DataTabledt = null;

private DataRowCollectiondr = null;

private String connString;

private String strSQL;

private void Page_Load(object sender, System.EventArgs e) {

if (!IsPostBack) {
connString = "data source=localhost;uid=test;pwd=test;initial catalog=Northwind";
strSQL = "SELECT LastName, FirstName, Title, HireDate FROM dbo.Employees";

try {
conn = new SqlConnection(connString);
cmd = new SqlCommand(strSQL, conn);
da = new SqlDataAdapter();
da.SelectCommand = cmd;
dt = new DataTable();
conn.Open();
da.Fill(dt);
dr = dt.Rows;

for (inti=0; i<=(dr.Count-1); i++) {
Response.Write(dr[i]["FirstName"].ToString()

+ " " + dr[i]["LastName"].ToString() + " - " +

dr[i]["Title"].ToString() + "<br>");

}
conn.Close();

} catch (SqlException ex) {
Response.Write("Error accessing database: " + ex.ToString());

} catch (Exception ex) {
Response.Write("Exception: " + ex.ToString());

} finally {

if (conn.State == ConnectionState.Open) {
conn.Close();

}
conn.Dispose();

} } }

</script>





<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Data" %>

<script language="C#" runat="server">

private SqlConnectionconn = null;

private SqlCommandcmd = null;

private SqlDataAdapterda = null;

private DataTabledt = null;

private DataRow[] dr = null;

private String connString;

private String strSQL;

private void Page_Load(object sender, System.EventArgs e) {

if (!IsPostBack) {
connString = "data source=localhost;uid=test;pwd=test;initial catalog=Northwind";
strSQL = "SELECT LastName, FirstName, Title, HireDate FROM dbo.Employees";

try {
conn = new SqlConnection(connString);
cmd = new SqlCommand(strSQL, conn);
da = new SqlDataAdapter();
da.SelectCommand = cmd;
dt = new DataTable();
conn.Open();
da.Fill(dt);
dgTest.DataSource = dt;
dgTest.DataBind();
conn.Close();

} finally {

if (conn.State == ConnectionState.Open) {
conn.Close();

}
conn.Dispose();

} } }

</script>

<asp:DataGrid id="dgTest" runat="Server"></asp:DataGrid>

No comments:

Post a Comment