Main steps of the experiment:
1: Create a new database name for Employdb, add a table employinfo, the field has name,sex,job,salary, and insert some data into it.
2: Create a new ASP.net program to drag and drop a GridView database control on the main interface.
3: Add the namespace: using System.Data.SqlClient;
Default.aspx
displaying database content with the GridView control
Lao Cai
Email:cxianfa@126.com
Main steps of the experiment:
1: Create a new database name for Employdb, add a table employinfo, the field has name,sex,job,salary, and insert some data into it.
2: Create a new ASP.net program to drag and drop a GridView database control on the main interface.
3: Add the namespace: using System.Data.SqlClient;
Default.aspx
| The code is as follows |
Copy Code |
|
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server"
<div>
< Asp:gridview id= "GridView1" runat= "server"
</asp:gridview>
</div>
</form>
< /body>
Default.aspx.cs
Using System;
using System.Data;
using System.Configuration;
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
{
SqlConnection sqlcon;
String strcon = "Data source=l ocalhost;database=employdb;uid=sa;pwd=; ";
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack) {
Bind ();
}
}
public void Bind () {
String sqlstr = ' Select Top 10 * from Employinfo ';
Sqlcon = new Sqlconnectio N (Strcon);
SqlDataAdapter SDA = new SqlDataAdapter (Sqlstr, Sqlcon); The
DataSet ds = new DataSet ();
Sqlcon. Open ();
SDA. Fill (ds, "Employinfo");
Gridview1.datasource = ds;
Gridview1.databind ();
Sqlcon. Close ();
}
}
|