DataSet architecture
DataAdapter architecture
ADO.NET模式的两种数据连接模式
? Connected data access
执行步骤如下
1. Establish a connection with the database.
2. Fetch a set of records in a cursor.
3. Work with the fetched data (perform read, modify, and delete operations or even calculations).
4. Update the database, if there are any changes.
5. Close the database connection.
适合使用的情景
? You are developing applications that are online all the time. For example, in a ticket reservation
application it is necessary that you work with the latest data from the database.
In such cases, connected data access becomes necessary.
? You want to avoid the overhead of using offline data. When you use queries directly
against a database, naturally they bypass any of the intermediate layers that are involved
in disconnected data-access techniques. For example, suppose that you wish to display a
simple employee listing to the end user. This task does not involve any processing as such.
Using connected data access in such cases will of course give the best performance.
? You need a cursor model for some reason.
示例代码
1: protected void Button1_Click(object sender, EventArgs e)
2: {
3: SqlConnection cnn = new SqlConnection(@"data source=.;initial catalog=northwind;integrated security=true");
4: SqlCommand cmd = new SqlCommand();
5: cmd.Connection = cnn;
6: cmd.CommandType = CommandType.Text;
7: cmd.CommandText = TextBox1.Text + " FOR XML AUTO";
8: cnn.Open();
9: //Connected Data Access关键代码
10: XmlReader reader = cmd.ExecuteXmlReader();
11: StreamWriter writer = File.CreateText(@"\temp.xml");
12: writer.Write("<root>");
13: while (reader.Read())
14: {
15: writer.Write(reader.ReadOuterXml());
16: }
17: writer.Write("</root>");
18: writer.Close();
19: reader.Close();
20: cnn.Close();
21: Process.Start(@"\temp.xml");
22: }
<pre class="alt"><span class="asp"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %></span></pre>
<pre> </pre>
<pre class="alt"><span class="kwrd"><!</span><span class="html">DOCTYPE</span> <span class="attr">html</span> <span class="attr">PUBLIC</span> <span class="kwrd">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span class="kwrd">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span class="kwrd">></span></pre>
<pre><span class="kwrd"><</span><span class="html">html</span> <span class="attr">xmlns</span><span class="kwrd">="http://www.w3.org/1999/xhtml"</span><span class="kwrd">></span></pre>
<pre class="alt"><span class="kwrd"><</span><span class="html">head</span> <span class="attr">runat</span><span class="kwrd">="server"</span><span class="kwrd">></span></pre>
<pre> <span class="kwrd"><</span><span class="html">title</span><span class="kwrd">></</span><span class="html">title</span><span class="kwrd">></span></pre>
<pre class="alt"><span class="kwrd"></</span><span class="html">head</span><span class="kwrd">></span></pre>
<pre><span class="kwrd"><</span><span class="html">body</span><span class="kwrd">></span></pre>
<pre class="alt"> <span class="kwrd"><</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">="form1"</span> <span class="attr">runat</span><span class="kwrd">="server"</span><span class="kwrd">></span></pre>
<pre> <span class="kwrd"><</span><span class="html">div</span><span class="kwrd">></span></pre>
<pre class="alt"> Execute Select Query:</pre>
<pre> <span class="kwrd"><</span><span class="html">asp:TextBox</span> <span class="attr">ID</span><span class="kwrd">="TextBox1"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="attr">Height</span><span class="kwrd">="21px"</span> <span class="attr">Width</span><span class="kwrd">="371px"</span><span class="kwrd">></</span><span class="html">asp:TextBox</span><span class="kwrd">></span></pre>
<pre class="alt"> <span class="kwrd"><</span><span class="html">br</span> <span class="kwrd">/></span></pre>
<pre> <span class="kwrd"><</span><span class="html">asp:Button</span> <span class="attr">ID</span><span class="kwrd">="Button1"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="attr">Text</span><span class="kwrd">="Execute"</span> <span class="attr">OnClick</span><span class="kwrd">="Button1_Click"</span> <span class="kwrd">/></span></pre>
<pre class="alt"> <span class="kwrd"><</span><span class="html">br</span> <span class="kwrd">/></span></pre>
<pre> Result XML File:<span class="kwrd"></</span><span class="html">div</span><span class="kwrd">></span></pre>
<pre class="alt"> <span class="kwrd"><</span><span class="html">asp:TextBox</span> <span class="attr">ID</span><span cl<p class='post-footer'>
original link:
<a href='http://Apiaceae.github.io/blog/2009/05/11/XML%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%28%E5%85%AB%29%EF%BC%9AXML%E4%B8%8EADO.NET/'>http://Apiaceae.github.io/blog/2009/05/11/XML%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%28%E5%85%AB%29%EF%BC%9AXML%E4%B8%8EADO.NET/</a><br/>
written by <a href='http://Apiaceae.github.io'>Hooker</a>
posted at <a href='http://Apiaceae.github.io'>http://Apiaceae.github.io</a>
</p>