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

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();
        }

    }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjffps.html