夜鹰教程网-程序员的加油站
 当前位置:文章中心 >> vs2022_vs2019_vs2017_vs2014_vs2012
DataSet 添加数据集、行、列、主键和外键等操作示例
夜鹰教程网 来源:www.yyjcw.com 日期:2016-10-27 17:12:30
DataSet 添加数据集、行、列、主键和外键等操作示例
前台代码:html
<%@ Page language="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.CodeUse" %>
<!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="" 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 窗体设计器所必需的。
            //
            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);
            }
       
        }
        //添加唯一键
        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();

复制链接 网友评论 收藏本文 关闭此页
上一条: .netFramework1.1下创建文件几种方…  下一条: ASP.NET 2.0–善用DataSourceMode属性
夜鹰教程网成立于2008年,目前已经运营了将近 13 年,发布了大量关于 html5/css3/C#/asp.net/java/python/nodejs/mongodb/sql server/android/javascript/mysql/mvc/easyui/vue/echarts原创教程。 我们一直都在坚持的是:认证负责、一丝不苟、以工匠的精神来打磨每一套教程,让读者感受到作者的用心。我们默默投入的时间,确保每一套教程都是一件作品,而不是呆板的文字和视频! 目前我们推出在线辅导班试运营,模式为一对一辅导,教学工具为QQ。我们的辅导学科包括 java 、android原生开发、webapp开发、商城开发、C#和asp.net开发,winform和物联网开发、web前端开发,但不仅限于此。 普通班针对的是国内学员,例如想打好基础的大学生、想转行的有志青年、想深入学习的程序员、想开发软件的初学者或者业余爱好者等。 就业办针对即将毕业上岗的大四学生,或者打算转行的初级开发工程师。 留学生班针对的是在欧美、加拿大、澳洲、日本、韩国、新加坡等地留学的中国学子,目的是让大家熟练地掌握编程技能,按时完成老师布置的作业,并能顺利地通过考试。 详细咨询QQ:1416759661   夜鹰教程网  基于角色的权限管理系统(c-s/b-s)。
  夜鹰教程网  基于nodejs的聊天室开发视频教程
  夜鹰教程网  Git分布式版本管理视频教程
  夜鹰教程网  MVC+EasyUI视频教程
  夜鹰教程网  在线考试系统视频教程
  夜鹰教程网  MongoDB视频教程。
  夜鹰教程网 Canvas视频教程
  夜鹰教程网 报表开发视频教程
  热点推荐
一个关于天气预报的WebService【C…
VS2010最大的新特点是并行编程的进…
TextBox控件:asp.net中如何为密码…
Web服务调用实例:实现天气预报的…
ASP.NET程序员面试试题(130道题)
ASP.NET教程:调用WebService的源码…
网站开发全程设计
据说这套.net面试题很多网络公司都…
考考你:C#常见题型及部分答案
原创:.net读取数据库sql2000
伪静态URL重写配置
配置web.config代码asp.net3.5个性…
使用线程池提高性能 Socket网络编…
ASP.NET(C#)GridView表头的增加…
如何找到正确的学习方向【.NET版】…
  最近更新
C#修改注册表demo
一个获取内容中的图片地址的方法
ASP.NET 4.0尚未在 Web 服务器上注…
四大作用域:application,session…
ConfigurationManager不存在的解决…
vs2012_vs2013_vs2015没有Web Dep…
vs2015禁用解决方案中单击打开文件…
微软为Visual Studio 2015新增安卓…
C#如何实现搜索引擎网络爬虫程序
C#中正则表达式的用法
用C#抓取需要登录的页面数据
VS2015新功能
VS2015安装图解教程
vs2015新功能介绍
vs2015安装图解

关于我们 | 网站建设 | 技术辅导 | 常见问题 | 联系我们 | 友情链接

夜鹰教程网 版权所有 www.yyjcw.com All rights reserved 备案号:蜀ICP备08011740号3