手工创建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>

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//调用可创建表和方法的关系
InBlock.gif
            DataSet ds = CreateDataSet();
InBlock.gif            
//将数据源的第一个表作为GridView的数据源
InBlock.gif
            Bugs.DataSource=ds.Tables["Bugs"];
InBlock.gif            Bugs.DataBind();
InBlock.gif            BugConstraints.DataSource
=ds.Tables["Bugs"].Constraints;
InBlock.gif            BugConstraints.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//手工创建
InBlock.gif
    private DataSet CreateDataSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//初始化新的DataSet对象,将使用表和关系填充对象
InBlock.gif
        DataSet dataSet = new DataSet();
InBlock.gif        
//以SQL数据表为依据来创建Bugs表及其列
InBlock.gif
        DataTable tblBugs = new DataTable("Bugs");
InBlock.gif        DataColumn newColumn;
InBlock.gif        newColumn 
= tblBugs.Columns.Add("BugID",Type.GetType("System.Int32"));
InBlock.gif        newColumn.AutoIncrement 
= true;//自动增加
InBlock.gif
        newColumn.AutoIncrementSeed = 1;//起始为1
InBlock.gif
        newColumn.AutoIncrementStep = 1;//步长为1
InBlock.gif
        newColumn.AllowDBNull = false;//不允许为空
InBlock.gif        
//设置一个命名的约束
InBlock.gif
        UniqueConstraint constraint = new UniqueConstraint("Unique_BugID", newColumn);
InBlock.gif        tblBugs.Constraints.Add(constraint);
InBlock.gif        
//为主键创建一个列数组
InBlock.gif
        DataColumn[] columnArray = new DataColumn[1];
InBlock.gif        columnArray[
0= newColumn;
InBlock.gif        
//为PrimaryKey属性添加该数组
InBlock.gif
        tblBugs.PrimaryKey = columnArray;
InBlock.gif        
//Product的列
InBlock.gif
        newColumn = tblBugs.Columns.Add("Product", Type.GetType("System.Int32"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.DefaultValue 
= 1;
InBlock.gif        DataColumn bugProductColumn 
= newColumn;
InBlock.gif        
//Version 列
InBlock.gif
        newColumn = tblBugs.Columns.Add("Version", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 50;
InBlock.gif        newColumn.DefaultValue 
= "0.1";
InBlock.gif        
//Description列
InBlock.gif
        newColumn = tblBugs.Columns.Add("Description", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 8000;
InBlock.gif        newColumn.DefaultValue 
= "";
InBlock.gif        
//Reporter列
InBlock.gif
        newColumn = tblBugs.Columns.Add("Reporter", Type.GetType("System.Int32"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        DataColumn bugReporterColumn 
= newColumn;
InBlock.gif        
//基于所有创建的数据库架构添加行
InBlock.gif
        DataRow newRow;//声明一个新行
InBlock.gif
        newRow = tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 1;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "Crashes on load";
InBlock.gif        newRow[
"Reporter"= 5;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif        newRow 
= tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 1;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "Does not report correct owner of bug";
InBlock.gif        newRow[
"Reporter"= 5;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 1;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "Fails to reload properly";
InBlock.gif        newRow[
"Reporter"= 6;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 1;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "Does not show history of previous action";
InBlock.gif        newRow[
"Reporter"= 5;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 2;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "Loses data overnight";
InBlock.gif        newRow[
"Reporter"= 5;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblBugs.NewRow();
InBlock.gif        newRow[
"Product"= 2;
InBlock.gif        newRow[
"Version"= "0.1";
InBlock.gif        newRow[
"Description"= "HTML is not shown properly";
InBlock.gif        newRow[
"Reporter"= 6;
InBlock.gif        tblBugs.Rows.Add(newRow);
InBlock.gif
InBlock.gif        
//将表添加到DataSet中
InBlock.gif
        dataSet.Tables.Add(tblBugs);
InBlock.gif        
//Product Table
InBlock.gif        
//实列化Products表并为其添加列
InBlock.gif
        DataTable tblProduct = new DataTable("lkProduct");
InBlock.gif        newColumn 
= tblProduct.Columns.Add("ProductID", Type.GetType("System.Int32"));
InBlock.gif        newColumn.AutoIncrement 
= true;//自动增加
InBlock.gif
        newColumn.AutoIncrementSeed = 1;//从1开始
InBlock.gif
        newColumn.AutoIncrementStep = 1;//增加1
InBlock.gif
        newColumn.AllowDBNull = false;//不允许为NULL
InBlock.gif
        newColumn.Unique = true;//每个值必须唯一
InBlock.gif
        newColumn = tblProduct.Columns.Add("ProductDescription", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 8000;
InBlock.gif        newColumn.DefaultValue 
= "";
InBlock.gif
InBlock.gif        newRow 
= tblProduct.NewRow();
InBlock.gif        newRow[
"ProductDescription"= "BugX Bug Tracking";
InBlock.gif        tblProduct.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblProduct.NewRow();
InBlock.gif        newRow[
"ProductDescription"= "PIM-MY Personal Information Manager";
InBlock.gif        tblProduct.Rows.Add(newRow);
InBlock.gif
InBlock.gif        
//将表添加到DataSet中
InBlock.gif
        dataSet.Tables.Add(tblProduct);
InBlock.gif        
//People
InBlock.gif
InBlock.gif        
//实列化People表,并为起添家列
InBlock.gif

InBlock.gif        DataTable tblPeople 
= new DataTable("People");
InBlock.gif        newColumn 
= tblPeople.Columns.Add("PersonID", Type.GetType("System.Int32"));
InBlock.gif        newColumn.AutoIncrement 
= true;//自动增加
InBlock.gif
        newColumn.AutoIncrementSeed = 1;//初始化为1
InBlock.gif
        newColumn.AutoIncrementStep = 1;//步长为1
InBlock.gif
        newColumn.AllowDBNull = false;//不允许为空
InBlock.gif
        UniqueConstraint uniqueConstraint = new UniqueConstraint("Unique_PersonID", newColumn);
InBlock.gif        tblPeople.Constraints.Add(uniqueConstraint);
InBlock.gif        
//将PersonID列作为外键约束
InBlock.gif
        DataColumn PersonIDColumn=newColumn;
InBlock.gif        columnArray 
= new DataColumn[1];
InBlock.gif        columnArray[
0= newColumn;
InBlock.gif        tblPeople.PrimaryKey 
= columnArray;
InBlock.gif
InBlock.gif        newColumn 
= tblPeople.Columns.Add("FullName", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 8000;
InBlock.gif        newColumn.DefaultValue 
= "";
InBlock.gif
InBlock.gif        newColumn 
= tblPeople.Columns.Add("eMail", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 100;
InBlock.gif        newColumn.DefaultValue 
= "";
InBlock.gif
InBlock.gif        newColumn 
= tblPeople.Columns.Add("Phone", Type.GetType("System.String"));
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif        newColumn.MaxLength 
= 20;
InBlock.gif        newColumn.DefaultValue 
= "";
InBlock.gif
InBlock.gif        newColumn 
= tblPeople.Columns.Add("Role", Type.GetType("System.Int32"));
InBlock.gif        newColumn.DefaultValue 
= 0;
InBlock.gif        newColumn.AllowDBNull 
= false;
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "Jesse Liberty";
InBlock.gif        newRow[
"email"= "jliberty@libertyassociater.com";
InBlock.gif        newRow[
"Phone"= "617-555-7301";
InBlock.gif        newRow[
"Role"= 1;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "Dan Hurwitz";
InBlock.gif        newRow[
"email"= "dhurwitz@libertyassociater.com";
InBlock.gif        newRow[
"Phone"= "781-555-3375";
InBlock.gif        newRow[
"Role"= 1;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "John Galt";
InBlock.gif        newRow[
"email"= "jGalt@franconia.com";
InBlock.gif        newRow[
"Phone"= "617-555-9876";
InBlock.gif        newRow[
"Role"= 1;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "Jesse Liberty";
InBlock.gif        newRow[
"email"= "jliberty@libertyassociater.com";
InBlock.gif        newRow[
"Phone"= "617-555-3232";
InBlock.gif        newRow[
"Role"= 3;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "ron petrusha";
InBlock.gif        newRow[
"email"= "ron@oreilly.com";
InBlock.gif        newRow[
"Phone"= "707-555-0515";
InBlock.gif        newRow[
"Role"= 2;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif
InBlock.gif        newRow 
= tblPeople.NewRow();
InBlock.gif        newRow[
"FullName"= "tatiana diaz";
InBlock.gif        newRow[
"email"= "tatiana@oreilly.com";
InBlock.gif        newRow[
"Phone"= "617-555-1234";
InBlock.gif        newRow[
"Role"= 2;
InBlock.gif        tblPeople.Rows.Add(newRow);
InBlock.gif
InBlock.gif        
//将People表添加到DataSet中
InBlock.gif
        dataSet.Tables.Add(tblPeople);
InBlock.gif        
//创建外键约束
InBlock.gif
        ForeignKeyConstraint fk = new ForeignKeyConstraint("FK_BugToPeople",PersonIDColumn,bugReporterColumn);
InBlock.gif        fk.DeleteRule 
= Rule.Cascade;
InBlock.gif        tblBugs.Constraints.Add(fk);
InBlock.gif
InBlock.gif
InBlock.gif        
//声明DataRelation和DataColumn对象
InBlock.gif
        System.Data.DataRelation dataRelation;
InBlock.gif        System.Data.DataColumn dataColumn1;
InBlock.gif        System.Data.DataColumn dataColumn2;
InBlock.gif
InBlock.gif        
//在Bug和BugHistory之间创建关系
InBlock.gif
        dataColumn1 = dataSet.Tables["People"].Columns["PersonID"];
InBlock.gif        dataColumn2 
= dataSet.Tables["Bugs"].Columns["Reporter"];
InBlock.gif        dataRelation 
= new System.Data.DataRelation("BugsToReporter", dataColumn1, dataColumn2);
InBlock.gif        dataSet.Relations.Add(dataRelation);
InBlock.gif        
return dataSet;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

转载于:https://www.cnblogs.com/liupan/archive/2007/04/03/698568.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值