手工创建DataTable

创建WEB应用程序.拖动两个GrieViewd对象到表单中.一个命名为Bugs.另一个命名BugConstraints



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="Bugs" runat="server">
        </asp:GridView>
   
    </div>
        <asp:GridView ID="BugConstraints" runat="server">
        </asp:GridView>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
//调用可创建表和方法的关系
            DataSet ds = CreateDataSet();
            
//将数据源的第一个表作为GridView的数据源
            Bugs.DataSource=ds.Tables["Bugs"];
            Bugs.DataBind();
            BugConstraints.DataSource
=ds.Tables["Bugs"].Constraints;
            BugConstraints.DataBind();
        }

    }

    
//手工创建
    private DataSet CreateDataSet()
    
{
        
//初始化新的DataSet对象,将使用表和关系填充对象
        DataSet dataSet = new DataSet();
        
//以SQL数据表为依据来创建Bugs表及其列
        DataTable tblBugs = new DataTable("Bugs");
        DataColumn newColumn;
        newColumn 
= tblBugs.Columns.Add("BugID",Type.GetType("System.Int32"));
        newColumn.AutoIncrement 
= true;//自动增加
        newColumn.AutoIncrementSeed = 1;//起始为1
        newColumn.AutoIncrementStep = 1;//步长为1
        newColumn.AllowDBNull = false;//不允许为空
        
//设置一个命名的约束
        UniqueConstraint constraint = new UniqueConstraint("Unique_BugID", newColumn);
        tblBugs.Constraints.Add(constraint);
        
//为主键创建一个列数组
        DataColumn[] columnArray = new DataColumn[1];
        columnArray[
0= newColumn;
        
//为PrimaryKey属性添加该数组
        tblBugs.PrimaryKey = columnArray;
        
//Product的列
        newColumn = tblBugs.Columns.Add("Product", Type.GetType("System.Int32"));
        newColumn.AllowDBNull 
= false;
        newColumn.DefaultValue 
= 1;
        DataColumn bugProductColumn 
= newColumn;
        
//Version 列
        newColumn = tblBugs.Columns.Add("Version", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 50;
        newColumn.DefaultValue 
= "0.1";
        
//Description列
        newColumn = tblBugs.Columns.Add("Description", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 8000;
        newColumn.DefaultValue 
= "";
        
//Reporter列
        newColumn = tblBugs.Columns.Add("Reporter", Type.GetType("System.Int32"));
        newColumn.AllowDBNull 
= false;
        DataColumn bugReporterColumn 
= newColumn;
        
//基于所有创建的数据库架构添加行
        DataRow newRow;//声明一个新行
        newRow = tblBugs.NewRow();
        newRow[
"Product"= 1;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "Crashes on load";
        newRow[
"Reporter"= 5;
        tblBugs.Rows.Add(newRow);
        newRow 
= tblBugs.NewRow();
        newRow[
"Product"= 1;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "Does not report correct owner of bug";
        newRow[
"Reporter"= 5;
        tblBugs.Rows.Add(newRow);

        newRow 
= tblBugs.NewRow();
        newRow[
"Product"= 1;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "Fails to reload properly";
        newRow[
"Reporter"= 6;
        tblBugs.Rows.Add(newRow);

        newRow 
= tblBugs.NewRow();
        newRow[
"Product"= 1;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "Does not show history of previous action";
        newRow[
"Reporter"= 5;
        tblBugs.Rows.Add(newRow);

        newRow 
= tblBugs.NewRow();
        newRow[
"Product"= 2;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "Loses data overnight";
        newRow[
"Reporter"= 5;
        tblBugs.Rows.Add(newRow);

        newRow 
= tblBugs.NewRow();
        newRow[
"Product"= 2;
        newRow[
"Version"= "0.1";
        newRow[
"Description"= "HTML is not shown properly";
        newRow[
"Reporter"= 6;
        tblBugs.Rows.Add(newRow);

        
//将表添加到DataSet中
        dataSet.Tables.Add(tblBugs);
        
//Product Table
        
//实列化Products表并为其添加列
        DataTable tblProduct = new DataTable("lkProduct");
        newColumn 
= tblProduct.Columns.Add("ProductID", Type.GetType("System.Int32"));
        newColumn.AutoIncrement 
= true;//自动增加
        newColumn.AutoIncrementSeed = 1;//从1开始
        newColumn.AutoIncrementStep = 1;//增加1
        newColumn.AllowDBNull = false;//不允许为NULL
        newColumn.Unique = true;//每个值必须唯一
        newColumn = tblProduct.Columns.Add("ProductDescription", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 8000;
        newColumn.DefaultValue 
= "";

        newRow 
= tblProduct.NewRow();
        newRow[
"ProductDescription"= "BugX Bug Tracking";
        tblProduct.Rows.Add(newRow);

        newRow 
= tblProduct.NewRow();
        newRow[
"ProductDescription"= "PIM-MY Personal Information Manager";
        tblProduct.Rows.Add(newRow);

        
//将表添加到DataSet中
        dataSet.Tables.Add(tblProduct);
        
//People

        
//实列化People表,并为起添家列

        DataTable tblPeople 
= new DataTable("People");
        newColumn 
= tblPeople.Columns.Add("PersonID", Type.GetType("System.Int32"));
        newColumn.AutoIncrement 
= true;//自动增加
        newColumn.AutoIncrementSeed = 1;//初始化为1
        newColumn.AutoIncrementStep = 1;//步长为1
        newColumn.AllowDBNull = false;//不允许为空
        UniqueConstraint uniqueConstraint = new UniqueConstraint("Unique_PersonID", newColumn);
        tblPeople.Constraints.Add(uniqueConstraint);
        
//将PersonID列作为外键约束
        DataColumn PersonIDColumn=newColumn;
        columnArray 
= new DataColumn[1];
        columnArray[
0= newColumn;
        tblPeople.PrimaryKey 
= columnArray;

        newColumn 
= tblPeople.Columns.Add("FullName", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 8000;
        newColumn.DefaultValue 
= "";

        newColumn 
= tblPeople.Columns.Add("eMail", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 100;
        newColumn.DefaultValue 
= "";

        newColumn 
= tblPeople.Columns.Add("Phone", Type.GetType("System.String"));
        newColumn.AllowDBNull 
= false;
        newColumn.MaxLength 
= 20;
        newColumn.DefaultValue 
= "";

        newColumn 
= tblPeople.Columns.Add("Role", Type.GetType("System.Int32"));
        newColumn.DefaultValue 
= 0;
        newColumn.AllowDBNull 
= false;

        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "Jesse Liberty";
        newRow[
"email"= "jliberty@libertyassociater.com";
        newRow[
"Phone"= "617-555-7301";
        newRow[
"Role"= 1;
        tblPeople.Rows.Add(newRow);

        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "Dan Hurwitz";
        newRow[
"email"= "dhurwitz@libertyassociater.com";
        newRow[
"Phone"= "781-555-3375";
        newRow[
"Role"= 1;
        tblPeople.Rows.Add(newRow);

        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "John Galt";
        newRow[
"email"= "jGalt@franconia.com";
        newRow[
"Phone"= "617-555-9876";
        newRow[
"Role"= 1;
        tblPeople.Rows.Add(newRow);

        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "Jesse Liberty";
        newRow[
"email"= "jliberty@libertyassociater.com";
        newRow[
"Phone"= "617-555-3232";
        newRow[
"Role"= 3;
        tblPeople.Rows.Add(newRow);

        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "ron petrusha";
        newRow[
"email"= "ron@oreilly.com";
        newRow[
"Phone"= "707-555-0515";
        newRow[
"Role"= 2;
        tblPeople.Rows.Add(newRow);


        newRow 
= tblPeople.NewRow();
        newRow[
"FullName"= "tatiana diaz";
        newRow[
"email"= "tatiana@oreilly.com";
        newRow[
"Phone"= "617-555-1234";
        newRow[
"Role"= 2;
        tblPeople.Rows.Add(newRow);

        
//将People表添加到DataSet中
        dataSet.Tables.Add(tblPeople);
        
//创建外键约束
        ForeignKeyConstraint fk = new ForeignKeyConstraint("FK_BugToPeople",PersonIDColumn,bugReporterColumn);
        fk.DeleteRule 
= Rule.Cascade;
        tblBugs.Constraints.Add(fk);


        
//声明DataRelation和DataColumn对象
        System.Data.DataRelation dataRelation;
        System.Data.DataColumn dataColumn1;
        System.Data.DataColumn dataColumn2;

        
//在Bug和BugHistory之间创建关系
        dataColumn1 = dataSet.Tables["People"].Columns["PersonID"];
        dataColumn2 
= dataSet.Tables["Bugs"].Columns["Reporter"];
        dataRelation 
= new System.Data.DataRelation("BugsToReporter", dataColumn1, dataColumn2);
        dataSet.Relations.Add(dataRelation);
        
return dataSet;
    }

}


 

posted on 2007-04-03 16:41  无限奔跑  阅读(4322)  评论(1编辑  收藏  举报