All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
使用javascript检测浏览器的相关特性_JavaScript技术_编程技术
delphi构造函数与析构函数的使用方法
ASP.NET,2.0中执行数据库操作命令之二_[Asp.Net教程]
Zend,Framework,1.0正式版即将发布_php资料_编程技术
最近收集的有用的Javscript小技巧_[Html教程]
Silverlight,2(beta1)数据操作(2)——使用ASP.NET,Web,Service_[Asp.Net教程]
用AJAX编写用户注册时的应用实例_.net资料_编程技术
PHP实现任意字符集下正常显示网页的方法_php资料_编程技术
asp.net2.0中web用户控件的概述与优点
ASP.NET技巧:数据导出到Excel最为简易的方法_[Asp.Net教程]
Reading number is top 10 pictures
Sora aoi mirror memorial classics5
In the world the most mysterious 21 place landscape4
Street street fighting
The real super beauty13
清扫五脏垃圾,我有绝招
Forced sex girl living abroad1
Photographed the passion of the clients and prostitutes in the sex trade picture1
Female star bikini
A man's favorite things1
西班牙山村小景1
Download software ranking
Eclipse-CALMSANNY (second edition)
WebService在.NET中的实战应用教学视频 → 第1集
Tram sex maniac 2 (H) rar bag3
Sora aoi‘s film--Lust fan wall
VeryCD电驴(EasyMule) V1.1.9 Build09081
Unix video tutorial10
Boxer's Top ten classic battle3
Unix video tutorial15
Ashlynn Video3
Unix video tutorial14
归海一刀 published in(发表于) 2014/1/30 1:56:59 Edit(编辑)
添加数据集(DataSet)、数据表(DataTable)、行、列、主键和外键等操作示例代码_[Asp.Net教程]

添加数据集(DataSet)、数据表(DataTable)、行、列、主键和外键等操作示例代码_[Asp.Net教程]

添加数据集(DataSet)、数据表(DataTable)、行、列、主键和外键等操作示例代码_[Asp.Net教程]

前台代码:HTML
<%@ Page language="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.CodeUse" %>



CodeUse










列号:
行号:





值:

表:

表一
表二




后台代码: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
{
/**////


/// CodeUse 的摘要说明。
///

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

/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.