public string GetBookTable()
{
using (SqlConnection conn = <nobr oncontextmenu="return false;" onmousemove="kwM(4);" id="key2" onmouseover="kwE(event,4, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">new</nobr> SqlConnection(ConfigurationSettings.AppSettings["MyConn"]))
{
using (SqlDataAdapter da = new SqlDataAdapter("select * from Book", conn))
{
DataSet ds = new DataSet();
da.Fill(ds, "Book");
StringBuilder strBuild = new StringBuilder();
StringWriter writer = new StringWriter(strBuild);
ds.WriteXml(writer, XmlWriteMode.IgnoreSchema);
return strBuild.ToString();
}
}
}
{
using (SqlConnection conn = <nobr oncontextmenu="return false;" onmousemove="kwM(4);" id="key2" onmouseover="kwE(event,4, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="_blank">new</nobr> SqlConnection(ConfigurationSettings.AppSettings["MyConn"]))
{
using (SqlDataAdapter da = new SqlDataAdapter("select * from Book", conn))
{
DataSet ds = new DataSet();
da.Fill(ds, "Book");
StringBuilder strBuild = new StringBuilder();
StringWriter writer = new StringWriter(strBuild);
ds.WriteXml(writer, XmlWriteMode.IgnoreSchema);
return strBuild.ToString();
}
}
}
本文介绍了一种使用C#语言从数据库中读取“Book”表所有记录的方法,并将这些记录转换为XML格式字符串的过程。该过程首先创建了一个数据库连接,然后使用SqlDataAdapter执行SQL查询来获取数据,最后通过DataSet对象将数据写入StringWriter,实现了数据的有效封装。
925

被折叠的 条评论
为什么被折叠?



