DataSet 添加数据集、行、列、主键和外键等操作示例

前台代码:html
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.CodeUse" %>
None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif < HTML >
None.gif < HEAD >
None.gif < title >CodeUse </ title >
None.gif < meta content ="Microsoft Visual Studio 7.0" name ="GENERATOR" >
None.gif < meta content ="C#" name ="CODE_LANGUAGE" >
None.gif < meta content ="JavaScript" name ="vs_defaultClientScript" >
None.gif < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
None.gif </ HEAD >
None.gif < body MS_POSITIONING ="GridLayout" >
None.gif < form id ="CodeUse" method ="post" runat ="server" >
None.gif < FONT face ="宋体" >
None.gif < asp:button id ="btnCreate" style ="Z-INDEX: 100; LEFT: 31px; POSITION: absolute; TOP: 43px" runat ="server" Height ="36" Width ="130" Text ="创建数据集和数据表" ></ asp:button >
None.gif < asp:Button id ="btnUpdateDs" style ="Z-INDEX: 117; LEFT: 35px; POSITION: absolute; TOP: 439px" runat ="server" Height ="36px" Width ="131px" Text ="赋值" ></ asp:Button >
None.gif < asp:Label id ="Label3" style ="Z-INDEX: 112; LEFT: 352px; POSITION: absolute; TOP: 395px" runat ="server" >列号: </ asp:Label >
None.gif < asp:Label id ="Label2" style ="Z-INDEX: 111; LEFT: 209px; POSITION: absolute; TOP: 395px" runat ="server" >行号: </ asp:Label >
None.gif < asp:Button id ="btnUpdateMID" style ="Z-INDEX: 107; LEFT: 37px; POSITION: absolute; TOP: 330px" runat ="server" Height ="36" Width ="131" Text ="修改MasterID" ></ asp:Button >
None.gif < asp:Button id ="btnAddForeign" style ="Z-INDEX: 106; LEFT: 35px; POSITION: absolute; TOP: 278px" runat ="server" Height ="36" Width ="131" Text ="添加外键" ></ asp:Button >
None.gif < asp:Button id ="Button1" style ="Z-INDEX: 105; LEFT: 31px; POSITION: absolute; TOP: 217px" runat ="server" Height ="36px" Width ="130px" Text ="添加唯一键" ></ asp:Button >< asp:button id ="btnAddRow" style ="Z-INDEX: 102; LEFT: 31px; POSITION: absolute; TOP: 161px" runat ="server" Height ="36px" Width ="130px" Text ="添加行" ></ asp:button >< asp:button id ="btnAddColumn" style ="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 102px" runat ="server" Height ="36px" Width ="130px" Text ="添加列" ></ asp:button >< asp:datagrid id ="dgMaster" style ="Z-INDEX: 103; LEFT: 242px; POSITION: absolute; TOP: 40px" runat ="server" Height ="120px" Width ="485px" ></ asp:datagrid >< asp:datagrid id ="dgChild" style ="Z-INDEX: 104; LEFT: 240px; POSITION: absolute; TOP: 193px" runat ="server" Height ="111px" Width ="479px" ></ asp:datagrid >
None.gif < asp:TextBox id ="tbRow" style ="Z-INDEX: 108; LEFT: 275px; POSITION: absolute; TOP: 395px" runat ="server" Width ="54px" ></ asp:TextBox >
None.gif < asp:TextBox id ="tbCol" style ="Z-INDEX: 109; LEFT: 422px; POSITION: absolute; TOP: 395px" runat ="server" Width ="64px" ></ asp:TextBox >
None.gif < asp:Label id ="Label1" style ="Z-INDEX: 110; LEFT: 520px; POSITION: absolute; TOP: 399px" runat ="server" >值: </ asp:Label >
None.gif < asp:TextBox id ="tbResult" style ="Z-INDEX: 113; LEFT: 585px; POSITION: absolute; TOP: 394px" runat ="server" Width ="145px" ></ asp:TextBox >
None.gif < asp:Label id ="Label4" style ="Z-INDEX: 114; LEFT: 209px; POSITION: absolute; TOP: 344px" runat ="server" >表: </ asp:Label >
None.gif < asp:DropDownList id ="ddlTable" style ="Z-INDEX: 115; LEFT: 277px; POSITION: absolute; TOP: 341px" runat ="server" >
None.gif < asp:ListItem Value ="0" >表一 </ asp:ListItem >
None.gif < asp:ListItem Value ="1" >表二 </ asp:ListItem >
None.gif </ asp:DropDownList >
None.gif < asp:Button id ="Button2" style ="Z-INDEX: 116; LEFT: 35px; POSITION: absolute; TOP: 389px" runat ="server" Height ="36px" Width ="131px" Text ="求值" ></ asp:Button ></ FONT ></ form >
None.gif </ body >
None.gif </ HTML >
None.gif
后台代码:cs
None.gif using System;
None.gif using System.Collections;
None.gif using System.ComponentModel;
None.gif using System.Data;
None.gif using System.Drawing;
None.gif using System.Web;
None.gif using System.Web.SessionState;
None.gif using System.Web.UI;
None.gif using System.Web.UI.WebControls;
None.gif using System.Web.UI.HtmlControls;
None.gif
None.gif namespace DsAndXML
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// CodeUse 的摘要说明。
ExpandedSubBlockEnd.gif
/// </summary>
InBlock.gifpublic class CodeUse : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifprotected System.Web.UI.WebControls.Button btnAddColumn;
InBlock.gifprotected System.Web.UI.WebControls.Button btnAddRow;
InBlock.gifprotected System.Web.UI.WebControls.DataGrid dgMaster;
InBlock.gifprotected System.Web.UI.WebControls.DataGrid dgChild;
InBlock.gifprotected System.Web.UI.WebControls.Button Button1;
InBlock.gifprotected System.Web.UI.WebControls.Button btnAddForeign;
InBlock.gifprotected System.Web.UI.WebControls.Button btnUpdateMID;
InBlock.gifprotected System.Web.UI.WebControls.TextBox tbRow;
InBlock.gifprotected System.Web.UI.WebControls.TextBox tbCol;
InBlock.gifprotected System.Web.UI.WebControls.Label Label1;
InBlock.gifprotected System.Web.UI.WebControls.Label Label2;
InBlock.gifprotected System.Web.UI.WebControls.Label Label3;
InBlock.gifprotected System.Web.UI.WebControls.TextBox tbResult;
InBlock.gifprotected System.Web.UI.WebControls.Label Label4;
InBlock.gifprotected System.Web.UI.WebControls.DropDownList ddlTable;
InBlock.gifprotected System.Web.UI.WebControls.Button Button2;
InBlock.gifprotected System.Web.UI.WebControls.Button btnUpdateDs;
InBlock.gifprotected System.Web.UI.WebControls.Button btnCreate;
InBlock.gif
InBlock.gifprivate void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
}
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gifWeb Form Designer generated code#region Web Form Designer generated code
InBlock.gifoverride protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif//
InBlock.gif
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif
//
InBlock.gif
InitializeComponent();
InBlock.gifbase.OnInit(e);
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif
/// 此方法的内容。
ExpandedSubBlockEnd.gif
/// </summary>
InBlock.gifprivate void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
InBlock.gifthis.btnAddRow.Click += new System.EventHandler(this.btnAddRow_Click);
InBlock.gifthis.btnAddColumn.Click += new System.EventHandler(this.btnAddColumn_Click);
InBlock.gifthis.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gifthis.btnAddForeign.Click += new System.EventHandler(this.btnAddForeign_Click);
InBlock.gifthis.btnUpdateMID.Click += new System.EventHandler(this.btnUpdateMID_Click);
InBlock.gifthis.Button2.Click += new System.EventHandler(this.Button2_Click);
InBlock.gifthis.btnUpdateDs.Click += new System.EventHandler(this.btnUpdateDs_Click);
InBlock.gifthis.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif#endregion
InBlock.gif
InBlock.gifprivate void btnCreate_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = new DataSet("myDS");//创建数据集
InBlock.gif
DataTable dtMaster = new DataTable("Master");//创建数据表
InBlock.gif
DataTable dtChild = new DataTable("Child");
InBlock.gif dsUntyped.Tables.Add(dtMaster);//把数据表添加到数据集中
InBlock.gif
dsUntyped.Tables.Add(dtChild);
InBlock.gif Session["ds"] = dsUntyped;
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void btnAddColumn_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif dsUntyped.Tables["Master"].Columns.Add("MasterID",typeof(int));
InBlock.gif dsUntyped.Tables["Master"].Columns.Add("MasterValue",typeof(string));
InBlock.gif dsUntyped.Tables["Child"].Columns.Add("MasterLink",typeof(int));
InBlock.gif dsUntyped.Tables["Child"].Columns.Add("ChildID",typeof(int));
InBlock.gif dsUntyped.Tables["Child"].Columns .Add("ChildValue",typeof(string));
InBlock.gif//修改表头
InBlock.gif
dsUntyped.Tables["Master"].Columns["MasterID"].Caption = "主ID";
InBlock.gif dsUntyped.Tables["Master"].Columns["MasterValue"].Caption = "值";
InBlock.gif Session["ds"] = dsUntyped;
InBlock.gif Bind();
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void btnAddRow_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif//为Master表添加两行
InBlock.gif
DataRow dr = dsUntyped.Tables["Master"].NewRow();
InBlock.gif dr["MasterID"] = 1;
InBlock.gif dr["MasterValue"] = "One";
InBlock.gif dsUntyped.Tables["Master"].Rows.Add(dr);
InBlock.gif dr = dsUntyped.Tables["Master"].NewRow();
InBlock.gif dr["MasterID"] = 2;
InBlock.gif dr["MasterValue"] = "Two";
InBlock.gif dsUntyped.Tables["Master"].Rows.Add(dr);
InBlock.gif//为child表添加1行
InBlock.gif
dr = dsUntyped.Tables["Child"].NewRow();
InBlock.gif dr["MasterLink"] = 1;
InBlock.gif dr["ChildID"] = 1;
InBlock.gif dr["ChildValue"] = "ChildOne";
InBlock.gif dsUntyped.Tables["Child"].Rows.Add(dr);
InBlock.gif Session["ds"] = dsUntyped;
InBlock.gif Bind();
ExpandedSubBlockEnd.gif }
InBlock.gifcatch(Exception ee)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Response.Write(ee.Message);
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif//添加唯一键
InBlock.gif
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif System.Data.UniqueConstraint uc = new UniqueConstraint("unqi",dsUntyped.Tables["Master"].Columns["MasterID"]);
InBlock.gif dsUntyped.Tables["Master"].Constraints.Add(uc);
InBlock.gif Session["ds"] = dsUntyped;
ExpandedSubBlockEnd.gif }
InBlock.gifprivate void Bind()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif dgMaster.DataSource = dsUntyped.Tables["Master"].DefaultView;
InBlock.gif dgChild.DataSource = dsUntyped.Tables["Child"].DefaultView;
InBlock.gifthis.DataBind();
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void btnAddForeign_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif System.Data.ForeignKeyConstraint fc = new ForeignKeyConstraint("fc",dsUntyped.Tables["Master"].Columns["MasterID"],dsUntyped.Tables["Child"].Columns["MasterLink"]);
InBlock.gif dsUntyped.Tables["Child"].Constraints.Add(fc);
InBlock.gif Session["ds"] = dsUntyped;
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void btnUpdateMID_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gif dsUntyped.Tables["Master"].Rows[0]["MasterID"] = 4;
InBlock.gif Bind();
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void Button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gifint nIndexTb = int.Parse(ddlTable.SelectedItem.Value);
InBlock.gifint nIndexRow = int.Parse(tbRow.Text);
InBlock.gifint nIndexCol = int.Parse(tbCol.Text);
InBlock.gifobject obj = dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol];
InBlock.gif tbResult.Text = obj.ToString();
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifprivate void btnUpdateDs_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif DataSet dsUntyped = (DataSet)Session["ds"];
InBlock.gifint nIndexTb = int.Parse(ddlTable.SelectedItem.Value);
InBlock.gifint nIndexRow = int.Parse(tbRow.Text);
InBlock.gifint nIndexCol = int.Parse(tbCol.Text);
InBlock.gif dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol] = tbResult.Text;
InBlock.gif Session["ds"] = dsUntyped;
InBlock.gif Bind();
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif}
None.gif如何把利用DataSet的GetXml()方法获得的字符串再转成DataSet对象
None.gifDataSet ds = db.ExecSqlForDataSet( strSQL );
None.gif string strDS = ds.GetXml();
None.gif dot.gif dot.gif
None.gif // 如何把strDS转换成DataSet对象??
None.gif

None.gif--------------------------------------------------------------------------------
None.gif
None.gifString strDS = " dot.gif"
None.gifSystem.IO.StringReader reader = new System.IO.StringReader(strDS);
None.gifds.ReadXml(reader);
None.gifreader.Close();
None.gif
None.gif



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/13/254216.html,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值