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

前台代码:html  
<%@ page language="c#" codebehind="codeuse.aspx.cs" autoeventwireup="false" inherits="dsandxml.codeuse" %>   @}brd#m[本文 来源 于我 的学习 网网络编程.Net教程 WWW.GZU521.COM )@}brd#m
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >  
<html>  
    <head>  
        <title>codeuse</title>  
        <meta content="microsoft visual studio 7.0" name="generator">  
        <meta content="c#" name="code_language">  
        <meta content="javascript" name="vs_defaultclientscript">  
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">  
    </head>  
    <body ms_positioning="gridlayout">  
        <form id="codeuse" method="post" runat="server">  
            <font face="宋体">  
                <asp:button id="btncreate" style="z-index: 100; left: 31px; position: absolute; top: 43px" runat="server" height="36" width="130" text="创建数据集和数据表"></asp:button>  
                <asp:button id="btnupdateds" style="z-index: 117; left: 35px; position: absolute; top: 439px" runat="server" height="36px" width="131px" text="赋值"></asp:button>  
                <asp:label id="label3" style="z-index: 112; left: 352px; position: absolute; top: 395px" runat="server">列号:</asp:label>  
                <asp:label id="label2" style="z-index: 111; left: 209px; position: absolute; top: 395px" runat="server">行号:</asp:label>  
                <asp:button id="btnupdatemid" style="z-index: 107; left: 37px; position: absolute; top: 330px" runat="server" height="36" width="131" text="修改masterid"></asp:button>  
                <asp:button id="btnaddforeign" style="z-index: 106; left: 35px; position: absolute; top: 278px" runat="server" height="36" width="131" text="添加外键"></asp:button>  
                <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>  
                <asp:textbox id="tbrow" style="z-index: 108; left: 275px; position: absolute; top: 395px" runat="server" width="54px"></asp:textbox>  
                <asp:textbox id="tbcol" style="z-index: 109; left: 422px; position: absolute; top: 395px" runat="server" width="64px"></asp:textbox>  
                <asp:label id="label1" style="z-index: 110; left: 520px; position: absolute; top: 399px" runat="server">值:</asp:label>  
                <asp:textbox id="tbresult" style="z-index: 113; left: 585px; position: absolute; top: 394px" runat="server" width="145px"></asp:textbox>  
                <asp:label id="label4" style="z-index: 114; left: 209px; position: absolute; top: 344px" runat="server">表:</asp:label>  
                <asp:dropdownlist id="ddltable" style="z-index: 115; left: 277px; position: absolute; top: 341px" runat="server">  
                    <asp:listitem value="0">表一</asp:listitem>  
                    <asp:listitem value="1">表二</asp:listitem>  
                </asp:dropdownlist>  
                <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>  
    </body>  
</html>  
后台代码:cs  
using system;  
using system.collections;  
using system.componentmodel;  
using system.data;  
using system.drawing;  
using system.web;  
using system.web.sessionstate;  
using system.web.ui;  
using system.web.ui.webcontrols;  
using system.web.ui.htmlcontrols;  

namespace dsandxml  
{  
    /** <summary>  
    /// codeuse 的摘要说明。  
    /// </summary>  
    public class codeuse : system.web.ui.page  
    {  
        protected system.web.ui.webcontrols.button btnaddcolumn;  
        protected system.web.ui.webcontrols.button btnaddrow;  
        protected system.web.ui.webcontrols.datagrid dgmaster;  
        protected system.web.ui.webcontrols.datagrid dgchild;  
        protected system.web.ui.webcontrols.button button1;  
        protected system.web.ui.webcontrols.button btnaddforeign;  
        protected system.web.ui.webcontrols.button btnupdatemid;  
        protected system.web.ui.webcontrols.textbox tbrow;  
        protected system.web.ui.webcontrols.textbox tbcol;  
        protected system.web.ui.webcontrols.label label1;  
        protected system.web.ui.webcontrols.label label2;  
        protected system.web.ui.webcontrols.label label3;  
        protected system.web.ui.webcontrols.textbox tbresult;  
        protected system.web.ui.webcontrols.label label4;  
        protected system.web.ui.webcontrols.dropdownlist ddltable;  
        protected system.web.ui.webcontrols.button button2;  
        protected system.web.ui.webcontrols.button btnupdateds;  
        protected system.web.ui.webcontrols.button btncreate;  
      
        private void page_load(object sender, system.eventargs e)  
        {  
            // 在此处放置用户代码以初始化页面  
        }  

        web form designer generated code#region web form designer generated code  
        override protected void oninit(eventargs e)  
        {  
            //  
            // codegen:该调用是 asp.net web 窗体设计器所必需的。  
XT]z1?p_ 来 源 于 贵 州学 习网 网络编程.Net教程 WWw.gzU521.COm XT]z1?p_

            //  
            initializecomponent();  
            base.oninit(e);  
        }  
          
        /** <summary>  
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
        /// 此方法的内容。  
        /// </summary>  
        private void initializecomponent()  
        {      
            this.btncreate.click += new system.eventhandler(this.btncreate_click);  
            this.btnaddrow.click += new system.eventhandler(this.btnaddrow_click);  
            this.btnaddcolumn.click += new system.eventhandler(this.btnaddcolumn_click);  
            this.button1.click += new system.eventhandler(this.button1_click);  
            this.btnaddforeign.click += new system.eventhandler(this.btnaddforeign_click);  
            this.btnupdatemid.click += new system.eventhandler(this.btnupdatemid_click);  
            this.button2.click += new system.eventhandler(this.button2_click);  
            this.btnupdateds.click += new system.eventhandler(this.btnupdateds_click);  
            this.load += new system.eventhandler(this.page_load);  

        }  
        #endregion 

 

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

private void btncreate_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = new dataset("myds");//创建数据集  
            datatable dtmaster = new datatable("master");//创建数据表  
            datatable dtchild = new datatable("child");  
            dsuntyped.tables.add(dtmaster);//把数据表添加到数据集中  
            dsuntyped.tables.add(dtchild);  
            session["ds"] = dsuntyped;  
              
        }  

        private void btnaddcolumn_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            dsuntyped.tables["master"].columns.add("masterid",typeof(int));  
            dsuntyped.tables["master"].columns.add("mastervalue",typeof(string));  
            dsuntyped.tables["child"].columns.add("masterlink",typeof(int));  
            dsuntyped.tables["child"].columns.add("childid",typeof(int));  
            dsuntyped.tables["child"].columns .add("childvalue",typeof(string));  
            //修改表头  
            dsuntyped.tables["master"].columns["masterid"].caption = "主id";  
            dsuntyped.tables["master"].columns["mastervalue"].caption = "值";  
            session["ds"] = dsuntyped;  
            bind();  
        }  

        private void btnaddrow_click(object sender, system.eventargs e)  
        {  
            try  
            {  
                dataset dsuntyped = (dataset)session["ds"];  
                //为master表添加两行  
                datarow dr = dsuntyped.tables["master"].newrow();  
                dr["masterid"] = 1;  
                dr["mastervalue"] = "one";  
                dsuntyped.tables["master"].rows.add(dr);  
                dr = dsuntyped.tables["master"].newrow();  
                dr["masterid"] = 2;  
                dr["mastervalue"] = "two";  
                dsuntyped.tables["master"].rows.add(dr);  
                //为child表添加1行  
                dr = dsuntyped.tables["child"].newrow();  
                dr["masterlink"] = 1;  
                dr["childid"] = 1;  
                dr["childvalue"] = "childone";  
                dsuntyped.tables["child"].rows.add(dr);  
                session["ds"] = dsuntyped;  
                bind();  
            }  
            catch(exception ee)  
            {  
                response.write(ee.message);  
            }  
          
        }  
        //添加唯一键  
{€9iF6G [资 料 来源于 贵州 学习 网 网络编程.Net教程 WwW.GzU521.CoM ] {€9iF6G

        private void button1_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            system.data.uniqueconstraint uc = new uniqueconstraint("unqi",dsuntyped.tables["master"].columns["masterid"]);  
            dsuntyped.tables["master"].constraints.add(uc);  
            session["ds"] = dsuntyped;  
        }  
        private void bind()  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            dgmaster.datasource = dsuntyped.tables["master"].defaultview;  
            dgchild.datasource = dsuntyped.tables["child"].defaultview;  
            this.databind();  
        }  

        private void btnaddforeign_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            system.data.foreignkeyconstraint fc = new foreignkeyconstraint("fc",dsuntyped.tables["master"].columns["masterid"],dsuntyped.tables["child"].columns["masterlink"]);  
            dsuntyped.tables["child"].constraints.add(fc);  
            session["ds"] = dsuntyped;  

        }  

        private void btnupdatemid_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            dsuntyped.tables["master"].rows[0]["masterid"] = 4;  
            bind();  
        }  

        private void button2_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            int nindextb = int.parse(ddltable.selecteditem.value);  
            int nindexrow = int.parse(tbrow.text);  
            int nindexcol = int.parse(tbcol.text);  
            object obj = dsuntyped.tables[nindextb].rows[nindexrow][nindexcol];  
            tbresult.text = obj.tostring();  
              
        }  

        private void btnupdateds_click(object sender, system.eventargs e)  
        {  
            dataset dsuntyped = (dataset)session["ds"];  
            int nindextb = int.parse(ddltable.selecteditem.value);  
            int nindexrow = int.parse(tbrow.text);  
            int nindexcol = int.parse(tbcol.text);  
            dsuntyped.tables[nindextb].rows[nindexrow][nindexcol] = tbresult.text;  
            session["ds"] = dsuntyped;  
            bind();  
        }  
    }  
}如何把利用dataset的getxml()方法获得的字符串再转成dataset对象   
dataset ds = db.execsqlfordataset( strsql );  
string strds = ds.getxml();  

//如何把strds转换成dataset对象??  

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

string strds = ""  
system.io.stringreader reader = new system.io.stringreader(strds);  
ds.readxml(reader);  
reader.close();  
本文来自学习网(www.gzu521.com),原文地址:http://www.gzu521.com/campus/article/program/200612/122354_2.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值