程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> DataSet 添加數據集、行、列、主鍵和外鍵等操作示例

DataSet 添加數據集、行、列、主鍵和外鍵等操作示例

編輯:.NET實例教程
前台代碼: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" runat="server" Height="36" Width="130" Text="創建數據集和數據表"></ASP:button>
                <asp:Button id="btnUpdateDs" runat="server" Height="36px" Width="131px" Text="賦值"></ASP:Button>
                <asp:Label id="Label3" runat="server">列號:</ASP:Label>
                <asp:Label id="Label2" runat="server">行號:</ASP:Label>
                <asp:Button id="btnUpdateMID" runat="server" Height="36" Width="131" Text="修改MasterID"></ASP:Button>
                <asp:Button id="btnAddForeign" runat="server" Height="36" Width="131" Text="添加外鍵"></ASP:Button>
                <asp:Button id="Button1" runat="server" Height="36px" Width="130px" Text="添加唯一鍵"></asp:Button><ASP:button id="btnAddRow" runat="server" Height="36px" Wid
th="130px" Text="添加行"></asp:button><asp:button id="btnAddColumn" runat="server" Height="36px" Width="130px" Text="添加列"></asp:button><asp:datagrid id="dgMaster" runat="server" Height="120px" Width="485px"></asp:datagrid><asp:datagrid id="dgChild" runat="server" Height="111px" Width="479px"></ASP:datagrid>
                <asp:TextBox id="tbRow" runat="server" Width="54px"></ASP:TextBox>
                <asp:TextBox id="tbCol" runat="server" Width="64px"></ASP:TextBox>
                <asp:Label id="Label1" runat="server">值:</ASP:Label>
                <asp:TextBox id="tbResult" runat="server" Width="145px"></ASP:TextBox>
                <asp:Label id="Label4" runat="server">表:</ASP:Label>
                <ASP:DropDownList id="ddlTable" runat="server">
                    <asp:ListItem Value="0">表一</ASP:ListItem>
                    <asp:ListItem Value="1">表二</ASP:ListItem>
                </ASP:DropDownList>
                <asp:Button id="Button2" 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 Sy
stem.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();
 &
nbsp;          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["M
aster"].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.Ev
entArgs 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



         
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved