zoukankan      html  css  js  c++  java
  • 示例DataSet的构成组件,手工打造DataSet


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using System.Data.Common;
    using System.Data.SqlClient;

    /*--===--------------------[DataSet Create  Demo]----------------------===---
     * DataSet是DataTable和Relation的集合
     * DataTable是DataColumn、DataRow、Constraint的集合
     * 本示例测试构建DataSet的上述对象
     * 请更改添加记录行的ID值,测试主键和外键约束,未考虑Check约束
     * --===--------------------[DataSet Create  Demo]----------------------===---
    */
    namespace DataSet_Create
    {
        
    public partial class frmDataSetCreate : Form
        {
            DataSet ds; 
    //DataSet object

            
    public frmDataSetCreate()
            {
                InitializeComponent();
            }

            
    private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            
    private void btnCreateDataSet_Click(object sender, EventArgs e)
            {
                
    //------------ 创建DataTable对象 tblStudents
                
    //DataTable 包含3个集合,集合为分别是Columns,Rows,Constraints;  
                
    //对应的对象为DataColumn, DataRow,Constraint
                DataTable tblStudents = new DataTable("Students");
                
    // 添加列
                tblStudents.Columns.Add("ID"typeof(System.Int32));
                tblStudents.Columns.Add(
    "Name"typeof(System.String));
                tblStudents.Columns.Add(
    "Birthday"typeof(System.DateTime));
                tblStudents.Columns.Add(
    "Comment"typeof(System.String));
                tblStudents.Columns[
    "ID"].AutoIncrement = true;
                tblStudents.Columns[
    "ID"].AutoIncrementSeed = 1;
                tblStudents.Columns[
    "ID"].ReadOnly = true;
                
    //定义Constraint,如何定义Check约束?!
                tblStudents.PrimaryKey = new DataColumn[] { tblStudents.Columns["ID"] };

                
    //-------------创建DataTable对象:tblScore
                DataTable tblScore = new DataTable("Score");
                tblScore.Columns.Add(
    "ID"typeof(System.Int32));
                tblScore.Columns.Add(
    "studentID"typeof(System.Int32));
                tblScore.Columns.Add(
    "Lecture");
                tblScore.Columns.Add(
    "score"typeof(System.Double));
                tblScore.Columns[
    "ID"].AutoIncrement = true;
                tblScore.Columns[
    "ID"].AutoIncrementSeed = 1;
                tblScore.Columns[
    "ID"].ReadOnly = true;
                
    //定义约束:主键、外键
                tblScore.PrimaryKey = new DataColumn[] { tblScore.Columns["ID"] };
                
    //tblScore.Constraints.Add("fk_studentID", tblStudents.Columns["ID"], tblScore.Columns["studentID"]);
                
    //ForeignKeyConstraint fk_StudentID = new ForeignKeyConstraint(tblStudents.Columns["ID"], tblScore.Columns["studentID"]);
                
    //tblScore.Constraints.Add(fk_StudentID);


                
    //--------------创建DataSet对象
                ds = new DataSet();
                ds.Tables.Add(tblStudents);
                ds.Tables.Add(tblScore);
                
    //添加关系Relation
                ds.Relations.Add(tblStudents.Columns["ID"], tblScore.Columns["studentID"]);//外键约束,尝试修改下面的数据测试



                
    //添加行tblStudents
                DataRow row = tblStudents.NewRow();
                row[
    "Name"= "刘德华"; row["Birthday"= "1970-3-3";
                tblStudents.Rows.Add(row);
                row 
    = tblStudents.NewRow();
                row[
    "Name"= "张学友"; row["Birthday"= "1972-6-22";
                tblStudents.Rows.Add(row);
                row 
    = tblStudents.NewRow();
                row[
    "Name"= "郭富城"; row["Birthday"= "1968-2-17";
                tblStudents.Rows.Add(row);


                
    //添加数据tblScore
                row = tblScore.NewRow();
                row[
    "StudentID"= 2; row["Lecture"= "English"; row["score"= 98.5;
                tblScore.Rows.Add(row);
                row 
    = tblScore.NewRow();
                row[
    "StudentID"= 2; row["Lecture"= "Math"; row["score"= 86.5;
                tblScore.Rows.Add(row);
                row 
    = tblScore.NewRow();
                row[
    "StudentID"= 3; row["Lecture"= "English"; row["score"= 87;
                tblScore.Rows.Add(row);



                MessageBox.Show(
    "DataSet Created OK, you can Bind Data to the DataGridView now!""DataSet is DONE!");
            }

            
    private void btnBindTable0_Click(object sender, EventArgs e)
            {
                dgvStudents.DataSource 
    = ds.Tables[0].DefaultView;
            }

            
    private void btnBindTable1_Click(object sender, EventArgs e)
            {
                dgvStudents.DataSource 
    = ds.Tables[1].DefaultView;
            }
        }
    }
  • 相关阅读:
    几个常用的排序算法
    计算机网络的一丢丢知识点
    最小的k个数
    操作系统的一丢丢知识点
    MySQL一丢丢知识点的了解
    B+树
    重建二叉树
    Redis简介
    shell脚本常用案例-5.10
    笔记-网络学习-子网划分
  • 原文地址:https://www.cnblogs.com/flaaash/p/1979890.html
Copyright © 2011-2022 走看看