Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > 演示DataRowCollection类的使用
【标  题】:演示DataRowCollection类的使用
【关键字】:DataRowCollection
【来  源】:http://blog.csdn.net/luchuanbo/archive/2006/12/19/1449662.aspx

演示DataRowCollection类的使用

Your Ad Here

(摘录自《C#函数实用手册》冶金工业出版社)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable testDT = new DataTable("student");
            DataColumn testDC;
            DataColumn[] keys = new DataColumn[1];
            testDC = new DataColumn();
            testDC.DataType = Type.GetType("System.Int32");
            testDC.ColumnName = "ID";
            keys[0] = testDC;
            testDT.Columns.Add(testDC);
            testDC = new DataColumn();
            testDC.DataType = Type.GetType("System.String");
            testDC.ColumnName = "Name";
            testDT.Columns.Add(testDC);
            testDC = new DataColumn();
            testDC.DataType = Type.GetType("System.String");

            testDC.ColumnName = "School";
            testDT.Columns.Add(testDC);

            DataRow testDR = testDT.NewRow();
            testDR["ID"] = 1;
            testDR["Name"] = "Rose";
            testDR["School"] = "SCUT";
            testDT.Rows.Add(testDR);

            testDR = testDT.NewRow();
            testDR["ID"] = 2;
            testDR["Name"] = "Coke";
            testDR["School"] = "SCNU";
            testDT.Rows.Add(testDR);

            DataRowCollection testDRC;
            testDRC = testDT.Rows;
           
            testDR = testDT.NewRow();
            testDR["ID"] = 3;
            testDR["Name"] = "Mike";
            testDR["School"] = "SCUT";
            testDT.Rows.Add(testDR);

            DoPrint(testDT);

            testDT.PrimaryKey = keys;
            Console.WriteLine("是否有ID为1的记录?" + testDRC.Contains("1"));
            Console.WriteLine("是否有ID为5的记录?" + testDRC.Contains("5"));
            testDR = testDRC.Find("2");
            Console.WriteLine("ID为2的记录:");
            foreach (DataColumn dc in testDT.Columns)
            {
                Console.Write(testDR[dc] + "  ");
            }
            Console.WriteLine();
            Console.WriteLine("插入前表的数据:");
            DoPrint(testDT);
            Console.WriteLine("插入后表的数据:");
            testDR = testDT.NewRow();
            testDR[0] = 8;
            testDR[1] = "Nick";
            testDR[2] = "ZSU";
            testDRC.InsertAt(testDR, 1);
            DoPrint(testDT);

            Console.WriteLine("删除第一行记录:");
            testDRC.RemoveAt(0);
            DoPrint(testDT);
            Console.WriteLine("删除所有记录:");
            testDRC.Clear();
            DoPrint(testDT);
        }

        private static void DoPrint(DataTable tempDT)
        {
            Console.WriteLine("    " + tempDT.TableName + "表数据:");
            foreach (DataColumn dc in tempDT.Columns)
            {
                Console.Write(dc.ColumnName + "   ");
            }
            Console.WriteLine();
            foreach (DataRow dr in tempDT.Rows)
            {
                foreach (DataColumn dc in tempDT.Columns)
                {
                    Console.Write(dr[dc] + "    ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("------------------------------\n");
        }
    }
}

 

 

*************************************************

执行结果:

    student表数据:
ID   Name   School  

1    Rose    SCUT   

2    Coke    SCNU   

3    Mike    SCUT   

------------------------------

是否有ID为1的记录?True
是否有ID为5的记录?False
ID为2的记录:
2  Coke  SCNU 

插入前表的数据:
    student表数据:
ID   Name   School  

1    Rose    SCUT   

2    Coke    SCNU   

3    Mike    SCUT   

------------------------------

插入后表的数据:
    student表数据:
ID   Name   School  

1    Rose    SCUT   

8    Nick    ZSU   

2    Coke    SCNU   

3    Mike    SCUT   

------------------------------

删除第一行记录:
    student表数据:
ID   Name   School  

8    Nick    ZSU   

2    Coke    SCNU   

3    Mike    SCUT   

------------------------------

删除所有记录:
    student表数据:
ID   Name   School  

------------------------------ 

解决tomcat中administration web application 的安装:【上一篇】
演示DataRow类的使用:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • JSON劫持以及ASP.NET AJAX 1.0是如何避免这些攻击的
  • 你会用Windows的“同步”功能吗?
  • 用GDI+绘制了一个钟表盘的类似物,显示当前的时间
  • 分析函數之二
  • 配置ISA Server以检测外部攻击和入侵(1)
  • 常用算法——递归
  • 第一次亲密接触 Painter(7)
  • mx.utils 包之Collection&Iterator
  • GCC内嵌汇编的一些资料
  • 《Windows ClearCase For Base 客户端使用指南》
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.