许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

示例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;
        }
    }
}

 

 

posted on 2011-03-10 16:03  许明会  阅读(272)  评论(0编辑  收藏  举报