• DataAdapter对象


    DataAdapter对象用于从数据源中获取数据,填充DataSet中的表和约束,并将对DataSet的更改提交回数据源。
    DataAdapter对象有4个重要属性,即SelectCommand、InsertCommand、UpdateCommand和DeleteCommand,这四个属性都是Command对象。
    protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection conn 
    = new OleDbConnection();
            conn.ConnectionString 
    = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                
    "Data Source=" + Server.MapPath("person.mdb");
            conn.Open();
            
    string sql="select * from grade";
            OleDbDataAdapter da 
    = new OleDbDataAdapter(sql, conn);
            DataSet ds 
    = new DataSet();
            da.Fill(ds, 
    "grade");
            dg.DataSource 
    = ds.Tables["grade"].DefaultView;
            dg.DataBind();
            conn.Close();
        }

    通过DataAdapter对象的构造函数"OleDbDataAdapter(sql, conn)",传入数据库连接对象和SQL语句,如果要处理DataAdapter对象中的数据,需要将其传递给DataSet对象。语句"da.Fill(ds, "grade")"的功能是将da中的数据填充到DataSet对象中,并起一个别名"grade"。程序最后将DataSet对象中的数据和DataGrid进行绑定,然后输出。

    DataAdapter对象还可以通过4个属性传递数据,给4个属性赋值的对象必须是Command对象。如果Command对象执行的是Select语句,必须将对象传递给SelectCommand属性;如果是Update语句,则传递给UpdateCommand属性;如果是Insert语句,则传递给InsertCommand属性;如果是Delete语句,则传递给DeleteCommand属性。
    protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection conn 
    = new OleDbConnection();
            conn.ConnectionString 
    = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                
    "Data Source=" + Server.MapPath("person.mdb");
            
    string sql="select * from grade";
            OleDbCommand Comm 
    = new OleDbCommand(sql, conn);
            OleDbDataAdapter da 
    = new OleDbDataAdapter();
            da.SelectCommand 
    = Comm;
            conn.Open();
            DataSet ds 
    = new DataSet();
            da.Fill(ds, 
    "grade");
            dg.DataSource 
    = ds.Tables["grade"].DefaultView;
            dg.DataBind();
            conn.Close();
        }

    默认情况下,当Connection对象执行Open方法的时候,DataAdapter对象将自动调用SelectCommand属性。除了SelectCommand属性,其他3个属性都需要使用"ExecuteNonQuary()"方法调用。
    protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection Conn 
    = new OleDbConnection();
            Conn.ConnectionString 
    = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                
    "Data Source=" + Server.MapPath("person.mdb");
            Conn.Open();
            
    string strInsertSQL = "insert into grade values(13,'女','小唐',98,78,88)";
            
    string strSelectSQL="select * from grade";
            
    string strUpdateSQL = "update grade set 数学=60 where 学号=13";
            
    string strDeleteSQL = "delete from grade where 学号=13";
            
    //创建Command对象
            OleDbCommand InsertComm = new OleDbCommand(strInsertSQL, Conn);
            OleDbCommand SelectComm 
    = new OleDbCommand(strSelectSQL, Conn);
            OleDbCommand UpdateComm 
    = new OleDbCommand(strUpdateSQL, Conn);
            OleDbCommand DeleteComm 
    = new OleDbCommand(strDeleteSQL, Conn);
            
    //创建DataAdapter对象da
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand 
    = SelectComm;
            da.InsertCommand 
    = InsertComm;
            da.UpdateCommand 
    = UpdateComm;
            da.DeleteCommand 
    = DeleteComm;
            
    //创建并填充DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, 
    "grade1");
            da.DeleteCommand.ExecuteNonQuery(); 
            da.Fill(ds, 
    "grade2");
            da.InsertCommand.ExecuteNonQuery();
            da.Fill(ds, 
    "grade3");
            da.UpdateCommand.ExecuteNonQuery();
            da.Fill(ds, 
    "grade4");
            
    //将DataSet绑定到DataGrid控件
            dg1.DataSource = ds.Tables["grade1"].DefaultView;
            dg2.DataSource 
    = ds.Tables["grade2"].DefaultView;
            dg3.DataSource 
    = ds.Tables["grade3"].DefaultView;
            dg4.DataSource 
    = ds.Tables["grade4"].DefaultView;
            dg1.DataBind();
            dg2.DataBind();
            dg3.DataBind();
            dg4.DataBind();
            Conn.Close();
        }
  • 相关阅读:
    Codeforces 1244C The Football Season (解方程)
    BZOJ 2006 [NOI2010]超级钢琴(ST表+堆)
    51Nod 1681 公共祖先(dfs+树状数组/主席树)
    CF 1076E Vasya and a Tree(dfs+树状数组)
    CF 1076 D Edge Deletion(最短路径+bfs)
    CF Gym 102059I Game on Plane(sg函数)
    POJ 2311 Cutting Game(二维sg)
    POJ 2960 S-Nim(SG函数模板题)
    CF Gym 102059F Fake Plastic Trees
    CF Gym 102059H Fractions(思维题)
  • 原文地址:https://www.cnblogs.com/qixin622/p/756746.html
Copyright © 2020-2023  润新知