DataSet dsUntyped = new DataSet("myDS");//创建数据集
DataTable dtMaster = new DataTable("Master");//创建数据表
DataTable dtChild = new DataTable("Child");
dsUntyped.Tables.Add(dtMaster);//把数据表添加到数据集中
dsUntyped.Tables.Add(dtChild);
//添加列
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 = "值";
//为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);
//添加唯一键
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 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;
}
//使用XML
<?xml version="1.0" standalone="yes"?>
<dbGuest>
<User>
<Name>aaa</Name>
<City>shanghai</City>
<Email>aaa@263.net</Email>
<Message>ok</Message>
<STime>2004-07-12T00:00:00.0000000+08:00</STime>
</User>
<User>
<Name>shaoazhd</Name>
<City>beijing</City>
<Email>sss@22.net</Email>
<Message>afsa</Message>
<STime>2004-7-12 15:07:39</STime>
</User>
<User>
<Name>Guset</Name>
<City>上海</City>
<Email>sfaf@22.net</Email>
</User>
<User>
<Name>Guset</Name>
<City>上海</City>
<Email>ss@22.net</Email>
</User>
</dbGuest>
using System.Xml;
using System.Xml.XPath;
private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Name");
ddlName.Items.Clear();
for(int i=0;i<elemList.Count;i++)
ddlName.Items.Add(elemList[i].InnerXml);
}
private void btnQuery_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
lbEmail.Text = doc.SelectSingleNode("//User[Name='"+ddlName.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText;
}
private void btnChange_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList nodeList=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("Name");
if(node.Count>0)
{
if(node[0].InnerText==ddlName.SelectedItem.Text)
{
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="Email")//如果找到
{
xe2.InnerText=tbNewMail.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList xnl=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("Name");
if(node.Count>0)
{
if(node[0].InnerText==ddlName.SelectedItem.Text)
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNode root=xmlDoc.SelectSingleNode("dbGuest");//查找<dbGuest>
XmlElement xe1=xmlDoc.CreateElement("User");//创建一个<User>节点
XmlElement xesub1=xmlDoc.CreateElement("Name");
xesub1.InnerText="Guset";//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2=xmlDoc.CreateElement("City");
xesub2.InnerText="上海";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("Email");
xesub3.InnerText="ss@22.net";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
private void btnSubmit_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet("myDs");
if(!File.Exists(Server.MapPath("dbQuestion.xml")))
{
//以下创建数据库xml
DataTable dt = new DataTable("Question");
dt.Columns.Add("QuestionID",typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Age",typeof(int));
dt.Columns.Add("Email",typeof(string));
dt.Columns.Add("Item",typeof(string));
ds.Tables.Add(dt);
//ds.WriteXml(Server.MapPath("dbQuestion.xml"));
}
else
ds.ReadXml(Server.MapPath("dbQuestion.xml"));
//以下生成一条记录
DataRow dr = ds.Tables[0].NewRow();
dr["QuestionID"] = ds.Tables[0].Rows.Count+1;
dr["Name"] =tbName.Text.Trim();
dr["Age"] = tbAge.Text;
dr["Email"] = tbEmail.Text;
//以下保存用户的调查项目
DataSet dsItem = new DataSet("Item");
DataTable dtItem = new DataTable("dtItem");
dsItem.Tables.Add(dtItem);
dtItem.Columns.Add("ID",typeof(int));
dtItem.Columns.Add("Title",typeof(string));
dtItem.Columns.Add("Answer",typeof(string));
for(int i=1;i<=6;i++)
{
DataRow drItem = dtItem.NewRow();
drItem["ID"] =i;
drItem["Title"] = ((Label)this.FindControl("lbQuestion"+i.ToString())).Text;
RadioButtonList rbl = (RadioButtonList)this.FindControl("rbQuestion"+i.ToString());
for(int j=0;j<4;j++)
{
if(rbl.Items[j].Selected)
drItem["Answer"] = rbl.Items[j].Text;
}
dtItem.Rows.Add(drItem);
}
dr["Item"] = dsItem.GetXml();
ds.Tables[0].Rows.Add(dr);
//最后保存并转向
ds.WriteXml(Server.MapPath("dbQuestion.xml"));
Session["myDs"] = ds;
Response.Redirect("show.aspx");
}
}