前台代码:html
<%@ Halaman bahasa="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.CodeUse" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisi//EN" >
<HTML>
<KEPALA>
<title>Penggunaan Kode</title>
<meta content="Microsoft Visual Studio 7.0" nama="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema">
</KEPALA>
<badan MS_POSITIONING="GridLayout">
<form id="CodeUse" method="post" runat="server">
<FONT muka="宋体">
<asp:button id="btnCreate" style="Z-INDEX: 100; KIRI: 31px; POSISI: absolut; ATAS: 43px" runat="server" Tinggi="36" Lebar="130" Text="创建数据集和数据表"></asp:button>
<asp:Button id="btnUpdateDs" style="Z-INDEX: 117; KIRI: 35px; POSISI: absolut; ATAS: 439px" runat="server" Tinggi="36px" Lebar="131px" Text="赋值" ></asp:Tombol>
<asp:Label id="Label3" style="Z-INDEX: 112; KIRI: 352px; POSISI: absolut; ATAS: 395px" runat="server">列号:</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 111; KIRI: 209px; POSISI: absolut; ATAS: 395px" runat="server">行号:</asp:Label>
<asp:Button id="btnUpdateMID" style="Z-INDEX: 107; KIRI: 37px; POSISI: absolut; ATAS: 330px" runat="server" Tinggi="36" Lebar="131" Text="修改MasterID "></asp:Tombol>
<asp:Button id="btnAddForeign" style="Z-INDEX: 106; KIRI: 35px; POSISI: absolut; ATAS: 278px" runat="server" Tinggi="36" Lebar="131" Text="添加外键"></asp:Tombol>
<asp:Button id="Button1" style="Z-INDEX: 105; KIRI: 31px; POSISI: absolut; ATAS: 217px" runat="server" Tinggi="36px" Lebar="130px" Text="添加唯一键"></asp:Button><asp:button id="btnAddRow" style="Z-INDEX: 102; KIRI: 31px POSISI: absolut: 161px" runat="server" Tinggi = 36px" ="130px" Text="添加行"></asp:button><asp:button id="btnAddColumn" style="Z-INDEX: 101; KIRI: 31px; POSISI: absolut; ATAS: 102px" runat=" server" Tinggi="36px" Lebar="130px" Text="添加列"></asp:button><asp:datagrid id="dgMaster" style="Z-INDEX: 103; KIRI: 242px; ; ATAS: 40px" runat="server" Tinggi="120px" Lebar="485px"></asp:datagrid><asp:datagrid id="dgChild" style="Z-INDEX: 104; : mutlak; ATAS: 193px" runat="server" Tinggi="111px" Lebar="479px"></asp:datagrid>
<asp:TextBox id="tbRow" style="Z-INDEX: 108; KIRI: 275px; POSISI: absolut; ATAS: 395px" runat="server" width="54px"></asp:TextBox>
<asp:TextBox id="tbCol" style="Z-INDEX: 109; KIRI: 422px; POSISI: absolut; ATAS: 395px" runat="server" width="64px"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 110; KIRI: 520px; POSISI: absolut; ATAS: 399px" runat="server">值:</asp:Label>
<asp:TextBox id="tbResult" style="Z-INDEX: 113; KIRI: 585px; POSISI: absolut; ATAS: 394px" runat="server" width="145px"></asp:TextBox>
<asp:Label id="Label4" style="Z-INDEX: 114; KIRI: 209px; POSISI: absolut; ATAS: 344px" runat="server">表:</asp:Label>
<asp:DropDownList id="ddlTable" style="Z-INDEX: 115; KIRI: 277px; POSISI: absolut; ATAS: 341px" runat="server">
<asp:ListItem Value="0">表一</asp:ListItem>
<asp:ListItem Value="1">表二</asp:ListItem>
</asp:Daftar DropDown>
<asp:Button id="Button2" style="Z-INDEX: 116; KIRI: 35px; POSISI: absolut; ATAS: 389px" runat="server" Tinggi="36px" Lebar="131px" Text="求值"></asp:Tombol></FONT></form>
</tubuh>
</HTML>
后台代码:cs
menggunakan Sistem;
menggunakan Sistem.Koleksi;
menggunakan Sistem.ComponentModel;
menggunakan Sistem.Data;
menggunakan Sistem.Gambar;
menggunakan Sistem.Web;
menggunakan Sistem.Web.SessionState;
menggunakan Sistem.Web.UI;
menggunakan Sistem.Web.UI.WebControls;
menggunakan Sistem.Web.UI.HtmlControls;
ruang nama DsAndXML
{
/**//// <ringkasan>
/// Penggunaan Kode 的摘要说明。
/// </ringkasan>
Kode kelas publikPenggunaan : System.Web.UI.Page
{
dilindungi System.Web.UI.WebControls.Button btnAddColumn;
dilindungi System.Web.UI.WebControls.Button btnAddRow;
dilindungi System.Web.UI.WebControls.DataGrid dgMaster;
dilindungi Sistem.Web.UI.WebControls.DataGrid dgChild;
dilindungi System.Web.UI.WebControls.Button Button1;
dilindungi System.Web.UI.WebControls.Button btnAddForeign;
Sistem.Web.UI.WebControls.Button yang dilindungi btnUpdateMID;
dilindungi System.Web.UI.WebControls.TextBox tbRow;
dilindungi System.Web.UI.WebControls.TextBox tbCol;
dilindungi Sistem.Web.UI.WebControls.Label Label1;
dilindungi Sistem.Web.UI.WebControls.Label Label2;
dilindungi Sistem.Web.UI.WebControls.Label Label3;
dilindungi System.Web.UI.WebControls.TextBox tbResult;
dilindungi Sistem.Web.UI.WebControls.Label Label4;
dilindungi System.Web.UI.WebControls.DropDownList ddlTable;
dilindungi System.Web.UI.WebControls.Button Button2;
dilindungi System.Web.UI.WebControls.Button btnUpdateDs;
dilindungi System.Web.UI.WebControls.Button btnCreate;
private void Page_Load(pengirim objek, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Kode yang dibuat oleh Perancang Formulir Web#region Kode yang dibuat oleh Perancang Formulir Web
ganti void OnInit yang dilindungi (EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
Inisialisasi Komponen();
base.OnInit(e);
}
/**//// <ringkasan>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </ringkasan>
kekosongan pribadi InitializeComponent()
{
this.btnCreate.Click += Sistem baru.EventHandler(this.btnCreate_Click);
this.btnAddRow.Click += Sistem baru.EventHandler(this.btnAddRow_Click);
this.btnAddColumn.Click += Sistem baru.EventHandler(this.btnAddColumn_Click);
this.Button1.Click += Sistem baru.EventHandler(this.Button1_Click);
this.btnAddForeign.Click += Sistem baru.EventHandler(this.btnAddForeign_Click);
this.btnUpdateMID.Klik += Sistem baru.EventHandler(ini.btnUpdateMID_Click);
ini.Button2.Klik += Sistem baru.EventHandler(ini.Button2_Klik);
this.btnUpdateDs.Click += Sistem baru.EventHandler(this.btnUpdateDs_Click);
this.Load += Sistem baru.EventHandler(ini.Page_Load);
}
#endregion
private void btnCreate_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = new DataSet("myDS");//创建数据集
DataTable dtMaster = new DataTable("Master");//创建数据表
DataTable dtChild = new DataTable("Anak");
dsUntyped.Tables.Add(dtMaster);//把数据表添加到数据集中
dsUntyped.Tables.Add(dtChild);
Sesi["ds"] = dsTidak diketik;
}
private void btnAddColumn_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
dsUntyped.Tables["Master"].Columns.Add("MasterID",typeof(int));
dsUntyped.Tables["Master"].Columns.Add("MasterValue",typeof(string));
dsUntyped.Tables["Anak"].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 = "值";
Sesi["ds"] = dsTidak diketik;
Mengikat();
}
private void btnAddRow_Click(pengirim objek, System.EventArgs e)
{
mencoba
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
//为Master表添加两行
DataRow dr = dsUntyped.Tables["Master"].NewRow();
dr["ID Master"] = 1;
dr["MasterValue"] = "Satu";
dsUntyped.Tables["Master"].Rows.Add(dr);
dr = dsUntyped.Tables["Master"].NewRow();
dr["ID Master"] = 2;
dr["MasterValue"] = "Dua";
dsUntyped.Tables["Master"].Rows.Add(dr);
//为child表添加1行
dr = dsUntyped.Tables["Anak"].NewRow();
dr["MasterLink"] = 1;
dr["ID Anak"] = 1;
dr["Nilai Anak"] = "AnakSatu";
dsUntyped.Tables["Anak"].Baris.Tambahkan(dr);
Sesi["ds"] = dsTidak diketik;
Mengikat();
}
menangkap (Pengecualian ee)
{
Respon.Tulis(ee.Pesan);
}
}
//添加唯一键
private void Button1_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
System.Data.UniqueConstraint uc = new UniqueConstraint("unqi",dsUntyped.Tables["Master"].Columns["MasterID"]);
dsUntyped.Tables["Master"].Constraints.Add(uc);
Sesi["ds"] = dsTidak diketik;
}
kekosongan pribadi Bind()
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
dgMaster.DataSource = dsUntyped.Tables["Master"].DefaultView;
dgChild.DataSource = dsUntyped.Tables["Child"].DefaultView;
ini.DataBind();
}
private void btnAddForeign_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
System.Data.ForeignKeyConstraint fc = new ForeignKeyConstraint("fc",dsUntyped.Tables["Master"].Columns["MasterID"],dsUntyped.Tables["Child"].Columns["MasterLink"]);
dsUntyped.Tables["Anak"].Constraints.Add(fc);
Sesi["ds"] = dsTidak diketik;
}
private void btnUpdateMID_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
dsUntyped.Tables["Master"].Baris[0]["MasterID"] = 4;
Mengikat();
}
private void Button2_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
int nIndexTb = int.Parse(ddlTable.SelectedItem.Value);
int nIndexRow = int.Parse(tbRow.Teks);
int nIndexCol = int.Parse(tbCol.Teks);
objek obj = dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol];
tbResult.Teks = obj.ToString();
}
private void btnUpdateDs_Click(pengirim objek, System.EventArgs e)
{
DataSet dsUntyped = (DataSet)Sesi["ds"];
int nIndexTb = int.Parse(ddlTable.SelectedItem.Value);
int nIndexRow = int.Parse(tbRow.Teks);
int nIndexCol = int.Parse(tbCol.Teks);
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol] = tbResult.Text;
Sesi["ds"] = dsTidak diketik;
Mengikat();
}
}
}如何把利用DataSet的GetXml()方法获得的字符串再转成DataSet对象
Set Data ds = db.ExecSqlForDataSet( strSQL );
string strDS = ds.GetXml();
//如何把strDS转换成DataSet对象??
--------------------------------------------------- ------------------------------
String strDS = ""
Pembaca System.IO.StringReader = System.IO.StringReader(strDS) baru;
ds.ReadXml(pembaca);
pembaca.Tutup();
http://www.cnblogs.com/skylaugh/archive/2006/12/18/595646.html