时间:2021-05-28
前台代码:html
<%@Pagelanguage="c#"Codebehind="CodeUse.aspx.cs"AutoEventWireup="false"Inherits="DsAndXML.CodeUse"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>CodeUse</title>
<metacontent="MicrosoftVisualStudio7.0"name="GENERATOR">
<metacontent="C#"name="CODE_LANGUAGE">
<metacontent="JavaScript"name="vs_defaultClientScript">
<metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="CodeUse"method="post"runat="server">
<FONTface="宋体">
<asp:buttonid="btnCreate"style="Z-INDEX:100;LEFT:31px;POSITION:absolute;TOP:43px"runat="server"Height="36"Width="130"Text="创建数据集和数据表"></asp:button>
<asp:Buttonid="btnUpdateDs"style="Z-INDEX:117;LEFT:35px;POSITION:absolute;TOP:439px"runat="server"Height="36px"Width="131px"Text="赋值"></asp:Button>
<asp:Labelid="Label3"style="Z-INDEX:112;LEFT:352px;POSITION:absolute;TOP:395px"runat="server">列号:</asp:Label>
<asp:Labelid="Label2"style="Z-INDEX:111;LEFT:209px;POSITION:absolute;TOP:395px"runat="server">行号:</asp:Label>
<asp:Buttonid="btnUpdateMID"style="Z-INDEX:107;LEFT:37px;POSITION:absolute;TOP:330px"runat="server"Height="36"Width="131"Text="修改MasterID"></asp:Button>
<asp:Buttonid="btnAddForeign"style="Z-INDEX:106;LEFT:35px;POSITION:absolute;TOP:278px"runat="server"Height="36"Width="131"Text="添加外键"></asp:Button>
<asp:Buttonid="Button1"style="Z-INDEX:105;LEFT:31px;POSITION:absolute;TOP:217px"runat="server"Height="36px"Width="130px"Text="添加唯一键"></asp:Button><asp:buttonid="btnAddRow"style="Z-INDEX:102;LEFT:31px;POSITION:absolute;TOP:161px"runat="server"Height="36px"Width="130px"Text="添加行"></asp:button><asp:buttonid="btnAddColumn"style="Z-INDEX:101;LEFT:31px;POSITION:absolute;TOP:102px"runat="server"Height="36px"Width="130px"Text="添加列"></asp:button><asp:datagridid="dgMaster"style="Z-INDEX:103;LEFT:242px;POSITION:absolute;TOP:40px"runat="server"Height="120px"Width="485px"></asp:datagrid><asp:datagridid="dgChild"style="Z-INDEX:104;LEFT:240px;POSITION:absolute;TOP:193px"runat="server"Height="111px"Width="479px"></asp:datagrid>
<asp:TextBoxid="tbRow"style="Z-INDEX:108;LEFT:275px;POSITION:absolute;TOP:395px"runat="server"Width="54px"></asp:TextBox>
<asp:TextBoxid="tbCol"style="Z-INDEX:109;LEFT:422px;POSITION:absolute;TOP:395px"runat="server"Width="64px"></asp:TextBox>
<asp:Labelid="Label1"style="Z-INDEX:110;LEFT:520px;POSITION:absolute;TOP:399px"runat="server">值:</asp:Label>
<asp:TextBoxid="tbResult"style="Z-INDEX:113;LEFT:585px;POSITION:absolute;TOP:394px"runat="server"Width="145px"></asp:TextBox>
<asp:Labelid="Label4"style="Z-INDEX:114;LEFT:209px;POSITION:absolute;TOP:344px"runat="server">表:</asp:Label>
<asp:DropDownListid="ddlTable"style="Z-INDEX:115;LEFT:277px;POSITION:absolute;TOP:341px"runat="server">
<asp:ListItemValue="0">表一</asp:ListItem>
<asp:ListItemValue="1">表二</asp:ListItem>
</asp:DropDownList>
<asp:Buttonid="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
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceDsAndXML
{
///<summary>
///CodeUse的摘要说明。
///</summary>
publicclassCodeUse:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonbtnAddColumn;
protectedSystem.Web.UI.WebControls.ButtonbtnAddRow;
protectedSystem.Web.UI.WebControls.DataGriddgMaster;
protectedSystem.Web.UI.WebControls.DataGriddgChild;
protectedSystem.Web.UI.WebControls.ButtonButton1;
protectedSystem.Web.UI.WebControls.ButtonbtnAddForeign;
protectedSystem.Web.UI.WebControls.ButtonbtnUpdateMID;
protectedSystem.Web.UI.WebControls.TextBoxtbRow;
protectedSystem.Web.UI.WebControls.TextBoxtbCol;
protectedSystem.Web.UI.WebControls.LabelLabel1;
protectedSystem.Web.UI.WebControls.LabelLabel2;
protectedSystem.Web.UI.WebControls.LabelLabel3;
protectedSystem.Web.UI.WebControls.TextBoxtbResult;
protectedSystem.Web.UI.WebControls.LabelLabel4;
protectedSystem.Web.UI.WebControls.DropDownListddlTable;
protectedSystem.Web.UI.WebControls.ButtonButton2;
protectedSystem.Web.UI.WebControls.ButtonbtnUpdateDs;
protectedSystem.Web.UI.WebControls.ButtonbtnCreate;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
}
WebFormDesignergeneratedcode#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.btnCreate.Click+=newSystem.EventHandler(this.btnCreate_Click);
this.btnAddRow.Click+=newSystem.EventHandler(this.btnAddRow_Click);
this.btnAddColumn.Click+=newSystem.EventHandler(this.btnAddColumn_Click);
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.btnAddForeign.Click+=newSystem.EventHandler(this.btnAddForeign_Click);
this.btnUpdateMID.Click+=newSystem.EventHandler(this.btnUpdateMID_Click);
this.Button2.Click+=newSystem.EventHandler(this.Button2_Click);
this.btnUpdateDs.Click+=newSystem.EventHandler(this.btnUpdateDs_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidbtnCreate_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=newDataSet("myDS");//创建数据集
DataTabledtMaster=newDataTable("Master");//创建数据表
DataTabledtChild=newDataTable("Child");
dsUntyped.Tables.Add(dtMaster);//把数据表添加到数据集中
dsUntyped.Tables.Add(dtChild);
Session["ds"]=dsUntyped;
}
privatevoidbtnAddColumn_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(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();
}
privatevoidbtnAddRow_Click(objectsender,System.EventArgse)
{
try
{
DataSetdsUntyped=(DataSet)Session["ds"];
//为Master表添加两行
DataRowdr=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(Exceptionee)
{
Response.Write(ee.Message);
}
}
//添加唯一键
privatevoidButton1_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(DataSet)Session["ds"];
System.Data.UniqueConstraintuc=newUniqueConstraint("unqi",dsUntyped.Tables["Master"].Columns["MasterID"]);
dsUntyped.Tables["Master"].Constraints.Add(uc);
Session["ds"]=dsUntyped;
}
privatevoidBind()
{
DataSetdsUntyped=(DataSet)Session["ds"];
dgMaster.DataSource=dsUntyped.Tables["Master"].DefaultView;
dgChild.DataSource=dsUntyped.Tables["Child"].DefaultView;
this.DataBind();
}
privatevoidbtnAddForeign_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(DataSet)Session["ds"];
System.Data.ForeignKeyConstraintfc=newForeignKeyConstraint("fc",dsUntyped.Tables["Master"].Columns["MasterID"],dsUntyped.Tables["Child"].Columns["MasterLink"]);
dsUntyped.Tables["Child"].Constraints.Add(fc);
Session["ds"]=dsUntyped;
}
privatevoidbtnUpdateMID_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(DataSet)Session["ds"];
dsUntyped.Tables["Master"].Rows[0]["MasterID"]=4;
Bind();
}
privatevoidButton2_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(DataSet)Session["ds"];
intnIndexTb=int.Parse(ddlTable.SelectedItem.Value);
intnIndexRow=int.Parse(tbRow.Text);
intnIndexCol=int.Parse(tbCol.Text);
objectobj=dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol];
tbResult.Text=obj.ToString();
}
privatevoidbtnUpdateDs_Click(objectsender,System.EventArgse)
{
DataSetdsUntyped=(DataSet)Session["ds"];
intnIndexTb=int.Parse(ddlTable.SelectedItem.Value);
intnIndexRow=int.Parse(tbRow.Text);
intnIndexCol=int.Parse(tbCol.Text);
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol]=tbResult.Text;
Session["ds"]=dsUntyped;
Bind();
}
}
}如何把利用DataSet的GetXml()方法获得的字符串再转成DataSet对象
DataSetds=db.ExecSqlForDataSet(strSQL);
stringstrDS=ds.GetXml();
//如何把strDS转换成DataSet对象??
--------------------------------------------------------------------------------
StringstrDS=""
System.IO.StringReaderreader=newSystem.IO.StringReader(strDS);
ds.ReadXml(reader);
reader.Close();
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
建立外键的前提:本表的列必须与外键类型相同(外键必须是外表主键)。外键作用:使两张表形成关联,外键只能引用外表中的列的值!指定主键关键字:foreignkey(
1.添加主键altertablegoodsaddprimarykey(sid);2.添加外键altertableordersaddforeignkey(good
例如:1.创建,修改和删除数据表,字段,索引,主键,外键等。2.提供tableeditor来进行数据操作3.提供console来运行sql命令4.提供数据导出功
前言现在系统的各种业务是如此的复杂,数据都存在数据库中的各种表中,这个主键啊,那个外键啊,而表与表之间就依靠着这些主键和外键联系在一起。而我们进行业务操作时,就
--语句功能--数据操作SELECT--从数据库表中检索数据行和列INSERT--向数据库表添加新数据行DELETE--从数据库表中删除数据行UPDATE--更