Examples of adding datasets, rows, columns, primary keys, and foreign keys to dataset)

Source: Internet
Author: User

Front-end Code : 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 maxcompute 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 =" creating datasets and Data Tables "> </ASP: button>
<Asp: button id = "btnupdateds" style = "Z-INDEX: 117; left: 35px; position: absolute; top: 439px "runat =" server "Height =" 36px "width =" 131px "text =" assign value "> </ASP: button>
<Asp: Label id = "label3" style = "Z-INDEX: 112; left: 352px; position: absolute; top: 395px" runat = "server"> column number: </ASP: Label>
<Asp: Label id = "label2" style = "Z-INDEX: 111; left: 209px; position: absolute; top: 395px" runat = "server"> row: </ASP: label>
<Asp: button id = "btnupdatemid" style = "Z-INDEX: 107; left: 37px; position: absolute; top: 330px "runat =" server "Height =" 36 "width =" 131 "text =" Modify masterid "> </ASP: button>
<Asp: button id = "btnaddforeign" style = "Z-INDEX: 106; left: 35px; position: absolute; top: 278px "runat =" server "Height =" 36 "width =" 131 "text =" add foreign key "> </ASP: button>
<Asp: button id = "button1" style = "Z-INDEX: 105; left: 31px; position: absolute; top: 217px "runat =" server "Height =" 36px "width =" 130px "text =" add a unique key "> </ASP: button> <asp: button id = "btnaddrow" style = "Z-INDEX: 102; left: 31px; position: absolute; top: 161px "runat =" server "Height =" 36px "width =" 130px "text =" add row "> </ASP: button> <asp: button id = "btnaddcolumn" style = "Z-INDEX: 101; left: 31px; position: absolute; top: 102px "runat =" server "Height =" 36px "width =" 130px "text =" add column "> </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"> value: </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"> table: </ASP: label>
<Asp: dropdownlist id = "ddltable" style = "Z-INDEX: 115; left: 277px; position: absolute; top: 341px" runat = "server">
<Asp: listitem value = "0"> Table 1 </ASP: listitem>
<Asp: listitem value = "1"> Table 2 </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 =" evaluate "> </ASP: button> </font> </form>
</Body>
</Html>
Background code: 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>
/// Brief description of 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)
{
// Place user code here to initialize the page
}

Web Form Designer generated code # region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </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. button#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 );

}< BR ># endregion

private void btncreate_click (Object sender, system. eventargs e)
{< br> dataset dsuntyped = new dataset ("myds"); // create a dataset
datatable dtmaster = new datatable ("master "); // create a data table
datatable dtchild = new datatable ("child");
dsuntyped. tables. add (dtmaster); // Add the data table to the dataset
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 ));
// Modify the header
Dsuntyped. Tables ["master"]. Columns ["masterid"]. Caption = "Master ID ";
Dsuntyped. Tables ["master"]. Columns ["mastervalue"]. Caption = "value ";
Session ["ds"] = dsuntyped;
BIND ();
}

Private void btnaddrow_click (Object sender, system. eventargs E)
{
Try
{
Dataset dsuntyped = (Dataset) session ["ds"];
// Add two rows to the master table
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 );
// Add one row to the child table
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 );
}

}
// Add a unique key
Private void button#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)
{< br> dataset dsuntyped = (Dataset) session ["ds"];
dsuntyped. tables ["master"]. rows [0] ["masterid"] = 4;
BIND ();
}

private void button2_click (Object sender, system. eventargs e)
{< br> 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 ();
}
}
} How to convert a string obtained using the getxml () method of dataset into a DataSet object?
Dataset DS = dB. execsqlfordataset (strsql );
String strds = Ds. getxml ();

// How do I convert strds into a DataSet object ??

--------------------------------------------------------------------------------

String strds = ""
System. Io. stringreader reader = new system. Io. stringreader (strds );
DS. readxml (Reader );
Reader. Close ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.