许明会的计算机技术主页

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架构测试
            许明会    2007年12月13日 20:57:46
--===------------------------------------------===---
*/
using System;
using System.Data;
using System.Data.SqlClient;

namespace xumh
{
    
public class runMyApp
    {
        
static void Main()
        {
            
//准备DataSet和DataTable对象,并建立关联
            DataSet ds = new DataSet();
            DataTable dtMaster 
= new DataTable("Master");
            DataTable dtChild 
= new DataTable("Child");
            ds.Tables.Add(dtMaster);
            ds.Tables.Add(dtChild);
            
//为DataTable添加Columns
            dtMaster.Columns.Add("MasterID",typeof(int));
            dtMaster.Columns.Add(
"MasterValue",typeof(string));
            dtChild.Columns.Add(
"MasterLink",typeof(int));
            dtChild.Columns.Add(
"ChildID",typeof(int));
            dtChild.Columns.Add(
"ChildValue",typeof(string));
            
//修改DataTable的表头Caption
            
//dtMaster.Columns["MasterID"].Caption = "主ID";
            
//dtChild.Columns["MasterValue"].Caption="值";
            
//return;
            
//添加新行
            DataRow dr = dtMaster.NewRow(); //NewRow 方法
            dr["MasterID"= 1;
            dr[
"MasterValue"= "主表字段1";
            dtMaster.Rows.Add(dr);
            dr 
= dtMaster.NewRow();
            dr[
"MasterID"= 2;
            dr[
"MasterValue"= "主表字段2";
            dtMaster.Rows.Add(dr);
            
//-----
            dr = dtChild.NewRow();
            dr[
"MasterLink"= 1 ;
            dr[
"ChildID"= 1 ;
            dr[
"ChildValue"= "子表字段1";
            dtChild.Rows.Add(dr);
            
//添加唯一键
            System.Data.UniqueConstraint uc = 
                
new UniqueConstraint("uc_MasterID",dtMaster.Columns["MasterID"]);
            dtMaster.Constraints.Add(uc);
            
//添加外键
            System.Data.ForeignKeyConstraint fc = 
                
new ForeignKeyConstraint("fc_MasterID",
                    dtMaster.Columns[
"MasterID"],dtChild.Columns["MasterLink"]);
            dtChild.Constraints.Add(fc); 
//dtChild,ForeignKey Constraint
            
//取值
            foreach(DataRow drow in dtMaster.Rows)
                Console.WriteLine(
"{0}\t{1}",drow["MasterID"],drow["MasterValue"]);
            
//赋值,修改Master表的MasterID,导致Child表的MasterLink随之改变(外键约束)
            dtMaster.Rows[0]["MasterID"= 3;
            
foreach(DataRow drow in dtChild.Rows)
                Console.WriteLine(
"{0}\t{1}\t{2}",drow["MasterLink"],drow["ChildID"],drow["ChildValue"]);
            
//对DataSet进行遍历
            foreach(DataTable dt in ds.Tables)
            {
                
foreach(DataRow dataRow in dt.Rows)
                {    
                    
for(int i=0; i<dt.Columns.Count; i++)
                        Console.Write(
"{0}\t",dr[i]);
                    Console.WriteLine();
                }
                Console.WriteLine(
"----------------");
            }        
        }
    };
}

posted on 2007-12-13 20:59  许明会  阅读(259)  评论(0编辑  收藏  举报