虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

XML学习笔记(八):XML与ADO.NET

| Comments

DataSet architecture

2009-05-10_175702

DataAdapter architecture

2009-05-10_175821

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">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Default6.aspx.cs&quot; Inherits=&quot;Default6&quot; %&gt;</span></pre>

<pre>&#160;</pre>

<pre class="alt"><span class="kwrd">&lt;!</span><span class="html">DOCTYPE</span> <span class="attr">html</span> <span class="attr">PUBLIC</span> <span class="kwrd">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span class="kwrd">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span class="kwrd">&gt;</span></pre>

<pre><span class="kwrd">&lt;</span><span class="html">html</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;http://www.w3.org/1999/xhtml&quot;</span><span class="kwrd">&gt;</span></pre>

<pre class="alt"><span class="kwrd">&lt;</span><span class="html">head</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span></pre>

<pre>    <span class="kwrd">&lt;</span><span class="html">title</span><span class="kwrd">&gt;&lt;/</span><span class="html">title</span><span class="kwrd">&gt;</span></pre>

<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span></pre>

<pre><span class="kwrd">&lt;</span><span class="html">body</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;form1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span></pre>

<pre>    <span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        Execute Select Query:</pre>

<pre>        <span class="kwrd">&lt;</span><span class="html">asp:TextBox</span> <span class="attr">ID</span><span class="kwrd">=&quot;TextBox1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">Height</span><span class="kwrd">=&quot;21px&quot;</span> <span class="attr">Width</span><span class="kwrd">=&quot;371px&quot;</span><span class="kwrd">&gt;&lt;/</span><span class="html">asp:TextBox</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">br</span> <span class="kwrd">/&gt;</span></pre>

<pre>        <span class="kwrd">&lt;</span><span class="html">asp:Button</span> <span class="attr">ID</span><span class="kwrd">=&quot;Button1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Execute&quot;</span> <span class="attr">OnClick</span><span class="kwrd">=&quot;Button1_Click&quot;</span> <span class="kwrd">/&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">br</span> <span class="kwrd">/&gt;</span></pre>

<pre>        Result XML File:<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">    <span class="kwrd">&lt;</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/>
   &nbsp;written by <a href='http://Apiaceae.github.io'>Hooker</a>
   &nbsp;posted at <a href='http://Apiaceae.github.io'>http://Apiaceae.github.io</a>
   </p>

Comments