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

原创 2007年10月15日 10:18:00
 

        dataset ds=new dataset(); 
        DataRow dr = ds.Tables[0].NewRow();
        dr["Finishdate"] = finishdate;
        dr["Operator"] =Operator;
        dr["disp"] =disp;
        ds.Tables[0].Rows.Add(dr);

 

 

前台代码: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="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 窗体设计器所必需的。
            //
            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();
http://www.cnblogs.com/skylaugh/archive/2006/12/18/595646.html

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

 前台代码:html@ Page language="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndX...
  • chaobaojun
  • chaobaojun
  • 2009-05-10 11:56:00
  • 548

消息 1776,级别 16,状态 0,第 1 行 在被引用表 '产品类别' 中没有与外键 'FK_leibie1' 中的引用列列表匹配的主键或候选键。 消息 1750,级别 16,状态 0,第 1 行

首先将参照列设置为唯一
  • zhangge360
  • zhangge360
  • 2014-05-20 23:55:36
  • 3123

C#DataTable增加列和行,并添加到DataSet中,并初始化dev下拉框LookUpEdit

DataSet dsZQZW = new DataSet();             dsZQZW.Tables.Add("ZQZWTable");             dsZQZW.Tab...
  • guohao1949
  • guohao1949
  • 2014-04-03 10:09:53
  • 4006

MySql之DDL操作创建表(添加主键, 外键约束以及基本的数据类型)

MySql之DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
  • baidu_37107022
  • baidu_37107022
  • 2017-07-22 20:29:02
  • 820

Python进阶之MySQL数据库的主键和外键约束

SQL的主键和外键的作用:    外键取值规则:空值或参照的主键值。 (1)插入非空值时,如果主键表中没有这个值,则不能插入。 (2)更新时,不能改为主键表中没有的值。 (3)删除主键表记录时,你可...
  • m0_37338590
  • m0_37338590
  • 2017-10-23 20:21:03
  • 506

数据库建表的一些操作,包括主键外键约束等等

create table 成绩 ( 学号 char(11) references 学员信息(学号) not null, 课程 int references 课程(编号) not null, 考次 in...
  • hapure1
  • hapure1
  • 2016-06-22 15:49:36
  • 1117

sqlserver中 在被引用表 'A' 中没有与外键 'c2' 中的引用列列表匹配的主键或候选键

代码如下: create table ddd (     name varchar(10) not null,     age int not null,     constraint ...
  • liningzheng
  • liningzheng
  • 2015-05-06 17:39:25
  • 2171

Dataset中移除当前行,操作其它行

ds.Tables[0].Rows.RemoveAt(gridView1.FocusedRowHandle); //ds是Tables[0]中的所有数据,此时已被移除了一条//开始处理其余数据的操作R...
  • xunzaosiyecao
  • xunzaosiyecao
  • 2014-01-08 09:43:43
  • 924

为dataset手动添加自定义列

涉及到多表查询,而且其中两列中的数据是由数据库表中的几列经过计算所得出来的,而非数据库中原有的列。  public DataSet GetListNotPayment(string strWhere)...
  • suixufeng
  • suixufeng
  • 2011-07-26 14:57:14
  • 2316

外键约束的创建及例子

SQL FOREIGN KEY 约束 一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。 让我们通过一个例子来解释外键。请看下面两个表: "Persons" 表: ...
  • ljianhui
  • ljianhui
  • 2012-09-25 18:52:18
  • 9521
收藏助手
不良信息举报
您举报文章:DataSet 添加数据集、行、列、主键和外键等操作示例
举报原因:
原因补充:

(最多只允许输入30个字)