using System.Data;
using System.Data.SqlClient; //using System.Data.OleDb;下文中的Sql变为OleDb
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myDataReader();
myDataSet();
count();
}
public void myDataReader()
{//利用DataReader阅读类,执行数据的“只向前”的读取
//"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/access.mdb")
string strConn = "Data Source=aidd2008;Initial Catalog=i05;Integrated Security=SSPI;";
SqlConnection ConnAcc = new SqlConnection(strConn); //Sql链接类的实例化
ConnAcc.Open();//打开数据库
string strSQL = "SELECT * FROM sc order by cno desc";//SQL语句
SqlCommand cmd = new SqlCommand(strSQL, ConnAcc);//创建Command命令对象
SqlDataReader dr = cmd.ExecuteReader();//创建DataReader对象,并调用ExecuteReader从数据源检索行
while (dr.Read())//启动阅读器的Read方法,返回行的每一列
{
Label1.Text += "+" + dr["sno"];//数据读取
}
dr.Close();//关闭阅读器
ConnAcc.Close();//关闭数据库
}
public void myDataSet()
{//利用DataSet,DataAdapter读取数据
string strConn = "Data Source=localhost;Initial Catalog=i05;Integrated Security=SSPI;";
SqlConnection ConnAcc = new SqlConnection(strConn); //Sql链接类的实例化
ConnAcc.Open(); //打开数据库
string strSQL = "SELECT * FROM student order by sno desc "; //要执行的SQL语句
SqlDataAdapter da = new SqlDataAdapter(strSQL, ConnAcc); //创建DataAdapter数据适配器实例
DataSet ds = new DataSet(); //创建DataSet实例
da.Fill(ds, "112233"); //使用DataAdapter的Fill方法(填充),调用SELECT命令
GridView1.DataSource = ds.Tables["112233"].DefaultView; //设置数据源
GridView1.DataBind(); //绑定数据
ConnAcc.Close(); //关闭数据库
}
public void Button1_Click(object sender, EventArgs e)
{//插入、更新、删除
string strConn = "Data Source=localhost;Initial Catalog=i05;Integrated Security=SSPI;";
SqlConnection ConnAcc = new SqlConnection(strConn); //Sql链接类的实例化
ConnAcc.Open();//打开数据库
//string strID = System.Guid.NewGuid().ToString();
//string strTime = System.DateTime.Now.ToString();
string strSQL = "INSERT INTO student(sno,sname) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
// INSERT INTO table1、UPDATE table1 SET、DELETE FROM table1
SqlCommand cmd = new SqlCommand(strSQL, ConnAcc);//创建Command命令对象
cmd.ExecuteNonQuery();//执行命令
ConnAcc.Close();//关闭数据库
}
public void count()
{//统计:求和、总行数
string strConn = "Data Source=localhost;Initial Catalog=i05;Integrated Security=SSPI;";
SqlConnection ConnAcc = new SqlConnection(strConn); //Sql链接类的实例化
ConnAcc.Open();//打开数据库
string strSQL = "SELECT COUNT(*) FROM student";//SQL统计,SUM等等
SqlCommand cmd = new SqlCommand(strSQL, ConnAcc);//创建Command命令对象
int intNum = (int)cmd.ExecuteScalar();//得到统计数,SUM则用double
Label2.Text = intNum.ToString();
ConnAcc.Close();//关闭数据库
}
protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" PageSize="2" ShowHeader="False">
<PagerSettings PageButtonCount="2" FirstPageText="first" Mode="NumericFirstLast" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"sno")%><%#DataBinder.Eval(Container.DataItem,"sname")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
...