C#数据库操作(阅读器、数据库绑定、修改、统计、分页)

using System;
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();
  }
}

 

posted @ 2014-02-13 18:28  thxjcy  阅读(418)  评论(0编辑  收藏  举报