DataSet使用示例,包括DataTable、DataAdapter应用示例

----==============示例一===================

//【1】实例化DataTable并加入DataSet中
            //【1-1】传统分步方法
            DataTable dt = new DataTable("Student");
            ds.Tables.Add(dt);
            
            //【2】定义好表的结构:DataColumn
            //【2-1】传统分步方法

            DataColumn dc = new DataColumn();
            dc.ColumnName = "SNO";
            dc.DataType = System.Type.GetType("System.Int32");
            dt.Columns.Add(dc);
            //【2-2】推荐简单方法
            dt.Columns.Add(new DataColumn("SName", Type.GetType("System.String")));
            //【3】插入数据
            DataRow dr = dt.NewRow();
            dr["SNO"] = 95001;
            dr["SName"] = "张三";
            dt.Rows.Add(dr);

            dt.Rows.Add(new object[] { 95002, "王二" });
            dt.Rows.Add(new object[] { 95003, "李四" });
            dt.Rows.Add(new object[] { 95004, "王五" });
            dt.Rows.Add(new object[] { 95005, "赵六" });
            dt.Rows.Add(new object[] { 95006, "马七" });

            //绑定数据
            dgvStudent.DataSource = null;
            dgvStudent.DataSource = ds.Tables["Student"];

 

------===============示例二==================

             //【1】加入表
            ds.Tables.Add(new DataTable("Cource"));
            //【2】加入列
            ds.Tables["Cource"].Columns.Add(new DataColumn("CNO", Type.GetType("System.Int32")));
            ds.Tables["Cource"].Columns.Add(new DataColumn("CName", Type.GetType("System.String")));
            //【3】加入行数据
            ds.Tables["Cource"].Rows.Add(new object[] { 39001, "计算机" });
            ds.Tables["Cource"].Rows.Add(new object[] { 39002, "国际贸易" });
            ds.Tables["Cource"].Rows.Add(new object[] { 39003, "法律" });
            ds.Tables["Cource"].Rows.Add(new object[] { 39001, "英语" });
            //【4】绑定数据
            dgvCourse.DataSource = null;
            dgvCourse.DataSource = ds.Tables["Cource"];

------==================示例三=======================

             private string connString = ConfigurationManager.ConnectionStrings["connstring"].ToString();
            //实例化DataSet
            DataSet ds = new DataSet();

            //实例化连接对象
            SqlConnection conn = new SqlConnection(connString);
            string sql01 = "select SNO,SName from student";
            string sql02 = "select CNO,CName from course";
            string sql03 = "select SNO,CNO,Sorce from sorce";
            //实例化command
            SqlCommand cmd01 = new SqlCommand(sql01, conn);
            SqlCommand cmd02 = new SqlCommand(sql02, conn);
            SqlCommand cmd03 = new SqlCommand(sql03, conn);

            conn.Open();
            ds.Tables.Add(new DataTable("student"));
            ds.Tables.Add(new DataTable("course"));
            ds.Tables.Add(new DataTable("sorce"));
            //SqlDataReader objReader = cmd01.ExecuteReader(CommandBehavior.CloseConnection);
            ds.Tables["student"].Load(cmd01.ExecuteReader());
            ds.Tables["course"].Load(cmd02.ExecuteReader());
            ds.Tables["sorce"].Load(cmd03.ExecuteReader());

            ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["SNO"] };//设置主键

            conn.Close();

      //find查找(需要设置主键才可用)

       private void button4_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()))
            {
                MessageBox.Show("请输入学号!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
            }
            DataRow dr = ds.Tables["student"].Rows.Find(textBox1.Text.Trim());//按学号查找学生姓名
            if (dr is null)
            {
                MessageBox.Show("没有查到此学号!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
            }
            else
            {
                label5.Text = dr["SName"].ToString();
            }
        }

       //修改datatable

       private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables["student"].Rows.Find(txtSNO.Text.Trim());
            if (dr == null)
            {
                MessageBox.Show("没有查到该学号!");
                return;
            }
            else
            {
                dr.BeginEdit();
                dr["SName"] = txtName.Text.Trim();
                dr.EndEdit();

            }
        }

           //删除数据

           //方法1:Remove
                ds.Tables["student"].Rows.Remove(dr);
            }
            //方法2:RemoveAt
            for (int i = 0; i < ds.Tables["student"].Rows.Count; i++)
            {
                if (dr["SNO"] == ds.Tables["student"].Rows[i]["SNO"])
                {
                    ds.Tables["student"].Rows.RemoveAt(i);
                    break;
                }
            }
            //方法3:delete
            ds.Tables["student"].Rows.Find(txtSNO.Text.Trim()).Delete();
            ds.Tables["student"].AcceptChanges();

 

=======================DataTable基本操作=======================

            //复制表-表结构和数据
            DataTable dt01 = new DataTable();
            dt01 = ds.Tables["student"].Copy();
            //复制表-仅结构
            DataTable dt02 = new DataTable();
            dt02 = ds.Tables["student"].Clone();
            //清空数据
            dt02.Clear();
            //插入第一行数据
            dt02.ImportRow(ds.Tables["student"].Rows[0]);
            //排序
            ds.Tables["student"].DefaultView.Sort = "Age,Gender";
            DataTable dt03 = ds.Tables["student"].DefaultView.ToTable();

=======================DataView基本操作=======================

            //DataView
            //实例化DataView视图
            DataView dv = ds.Tables["student"].DefaultView;
            //把结果赋值给DataView
            dv.RowFilter = "SName like '%" + txtName.Text.Trim() + "%'";
            //把结果绑定到DataGridView
            dgvStudent.DataSource = dv;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值