• 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(
    "----------------");
                }        
            }
        };
    }
  • 相关阅读:
    C#水晶报表的分页统计字段
    ymPrompt消息提示组件js实现
    C#委托学习 原文推荐:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html?login=1#commentform
    C#之winfrom打印图片
    TreeView控件如何设置节点显示与隐藏,主要是用来做后台权限,没有权限的就隐藏,有权限的就显示?
    C#多线程间同步实例 原文:http://blog.csdn.net/zhoufoxcn/article/details/2453803
    C#反射的应用 原文摘自:http://blog.csdn.net/Tsapi/article/details/6234205
    C#编写的winform程序打包方法
    虚拟机下的CentOS环境中安装Node.js和npm
    RequireJS模块化与GruntJS构建
  • 原文地址:https://www.cnblogs.com/flaaash/p/994134.html
Copyright © 2020-2023  润新知