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

所属分类: 网络编程 / ASP.NET 阅读数: 2044
收藏 0 赞 0 分享
前台代码: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

        
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"
更多精彩内容其他人还在看

开源跨平台运行服务插件TaskCore.MainForm

这篇文章主要为大家详细介绍了开源跨平台运行服务插件TaskCore.MainForm的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

CKEditor自定义按钮插入服务端图片

这篇文章主要为大家详细介绍了CKEditor自定义按钮插入服务端图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net Web Api实现图片点击式图片验证码功能

现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码。下面通过本文给大家分享Asp.net Web Api实现图片点击式图片验证码功能,需要的的朋友参考下吧
收藏 0 赞 0 分享

WPF实现ScrollViewer滚动到指定控件处

这篇文章主要为大家详细介绍了WPF实现ScrollViewer滚动到指定控件处,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现带全选复选框的列表控件

这篇文章主要为大家详细介绍了WPF实现带全选复选框的列表控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net MVC 中利用jquery datatables 实现数据分页显示功能

这篇文章主要介绍了Asp.net MVC 中利用jquery datatables 实现数据分页显示功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net 利用NPOI导出Excel通用类的方法

本篇文章主要介绍了asp.net 利用NPOI导出Excel通用类的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2015自带LocalDB数据库用法详解

这篇文章主要为大家详细介绍了VS2015自带LocalDB数据库的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(一)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(二)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务的第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多