C# dataset

       表一定要有主键!!!!

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=pp");
            SqlDataAdapter da = new SqlDataAdapter("select * from tableww", con);
            ds = new DataSet();
            da.Fill(ds);
            this.gridControl1.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=pp");
            SqlDataAdapter da = new SqlDataAdapter("select * from tableww", con);
            cmb = new SqlCommandBuilder(da);
            da.UpdateCommand = cmb.GetUpdateCommand();
            da.InsertCommand = cmb.GetInsertCommand();
            da.DeleteCommand = cmb.GetDeleteCommand();
            da.Update(ds.GetChanges());
            ds.AcceptChanges();
        }

datatable的select方法

        private void repositoryItemRadioGroup1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            int[] i = gridView1.GetSelectedRows();

            DataRow[] dr = ds.Tables[0].Select("id =" + gridView1.GetDataRow(i[0])[0].ToString());
            foreach (DataRow d in dr)
            {
                d[2] = e.NewValue.ToString();
            }
        }

 

多表查询用容器嵌套

        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=qq");
        new SqlDataAdapter("select * from Users", conn).Fill(ds, "Users");
        new SqlDataAdapter("select * from Roles", conn).Fill(ds, "Roles");
        new SqlDataAdapter("select * from UserRoleJoin", conn).Fill(ds, "UserRoleJoin");
        //DataRelation
        DataRelation del1 = new DataRelation("UserRoleJoin", ds.Tables["Users"].Columns["UserId"], ds.Tables["UserRoleJoin"].Columns["UserId"], false);
        DataRelation del2 = new DataRelation("Roles", ds.Tables["UserRoleJoin"].Columns["RoleId"], ds.Tables["Roles"].Columns["RoleId"], false);

        ds.Relations.Add(del1);
        ds.Relations.Add(del2);

        this.DataList1.DataSource = ds.Tables["Users"].DefaultView;
        this.DataList1.DataBind();

        //((System.Data.DataRowView)Container.DataItem).CreateChildView("UserRoleJoin");

  • 1
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值