<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>script0r´s world | Paul Bartulej - Tutorial</title>
    <link>http://www.script0r.ndev.de/</link>
    <description>A World of Development and Programming</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.0.2 - http://www.s9y.org/</generator>
    <pubDate>Wed, 04 Apr 2007 10:42:03 GMT</pubDate>

    <image>
        <url>http://www.script0r.ndev.de/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: script0r´s world | Paul Bartulej - Tutorial - A World of Development and Programming</title>
        <link>http://www.script0r.ndev.de/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>DataSet / DataTable in 5 steps (SQL provided)</title>
    <link>http://www.script0r.ndev.de/index.php?/archives/13-DataSet-DataTable-in-5-steps-SQL-provided.html</link>
            <category>Programming</category>
            <category>Tutorial</category>
    
    <comments>http://www.script0r.ndev.de/index.php?/archives/13-DataSet-DataTable-in-5-steps-SQL-provided.html#comments</comments>
    <wfw:comment>http://www.script0r.ndev.de/wfwcomment.php?cid=13</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.script0r.ndev.de/rss.php?version=2.0&amp;type=comments&amp;cid=13</wfw:commentRss>
    

    <author>nospam@example.com (Paul Bartulej)</author>
    <content:encoded>
    DataSet or the DataTable are the offline version of your original Database. Usually, you need only either the DataSet or the DataTable - whereas mostly the DataTable. Once, you fill your Component, all the selected Data will be stored in your memory (in fact). Everytime, you need the current Data from the Database or you have to upload the Data in the Database, you need some steps to realize it. Of course, there are several ways but I will show you the mostly used one and it is really as easy as it sounds in the title &lt;img src=&quot;http://www.script0r.ndev.de/templates/default/img/emoticons/wink.png&quot; alt=&quot;;-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt; Note, that I am using the SQL Provoder. &lt;br /&gt;
&lt;br /&gt;
The Components, you need are:&lt;br /&gt;
&lt;br /&gt;
- a connection object&lt;br /&gt;
- a connection string&lt;br /&gt;
- a command object&lt;br /&gt;
- a command string&lt;br /&gt;
- a DataAdapter object&lt;br /&gt;
- and last but not least the DataSet or the DataTable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
First of all, you define the connecton object and the connection string, both provided by the System.Data.SqlClient, and assign the string to the object. &lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;SqlConnection SqlConn = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SqlConnection&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;SqlConnStr = &lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;Data Source=&amp;lt;Computername&amp;gt;&lt;span style=&quot;color: #840000; font-weight: bold;&quot;&gt;\\&lt;/span&gt;&amp;gt;Servername&amp;gt;;Initial Catalog=&amp;lt;DatabaseName&amp;gt;;&amp;quot;&lt;/span&gt; &lt;br /&gt;+ &lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;Integrated Security=True;Pooling=False&amp;quot;&lt;/span&gt;;&lt;br /&gt;SqlConn.&lt;span style=&quot;color: #000000;&quot;&gt;ConnectionString&lt;/span&gt; = SqlConnStr;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #218429;&quot;&gt;// alternate (whereas the connection string is commited by a parameter)&lt;/span&gt;&lt;br /&gt;SqlConnection SqlConn = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SqlConnection&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;Data Source=&amp;lt;Computername&amp;gt;&lt;span style=&quot;color: #840000; font-weight: bold;&quot;&gt;\\&lt;/span&gt;&amp;gt;Servername&amp;gt;;Initial Catalog=&amp;lt;DatabaseName&amp;gt;;&amp;quot;&lt;/span&gt; &lt;br /&gt;+ &lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;Integrated Security=True;Pooling=False&amp;quot;&lt;/span&gt;;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;
The connection string is divided into 4 Tags (in this case). The first is the Data Source - it means the computer, where the Server is running and, of course, the Servername on it. The second one is the name of your Database in the logged server (Initial Catalog). The integrated security means wheter the authentification is provided by the windows account usage or the server account (true is recommended and represents the windows account). Pooling is optional and says wether the pooling will be activated or not. &lt;br /&gt;
&lt;br /&gt;
The second step of our goal is to define the command object and the command text (string). Both, the command text and the connection object, we created above, have to be assigned to the command object. &lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;SqlCommand SqlSelectCmd = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SqlCommand&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;string&lt;/span&gt; SqlCmdTxt = &lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;SELECT * FROM northwind WHERE Acc_Name = &#039;Paul&#039; &amp;quot;&lt;/span&gt;;&lt;br /&gt;SqlCommand.&lt;span style=&quot;color: #000000;&quot;&gt;CommandText&lt;/span&gt; = SqlCmdTxt;&lt;br /&gt;SqlCommand.&lt;span style=&quot;color: #000000;&quot;&gt;Connection&lt;/span&gt; = SqlConn;&lt;/div&gt;&lt;br /&gt;
At this point, lets create the DataAdapter and the DataTable.&lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;DataAdapter da = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; DataAdapter&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;DataTable dtAccount = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; DataTable&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;
Note, that the DataAdapter associate the DataTabe with the Database.&lt;br /&gt;
&lt;br /&gt;
DataTable / DataSet   &lt;----- DataAdapter ------&gt; DataBase &lt;br /&gt;
&lt;br /&gt;
The DataTable will be filled by the DataAdapter with data from the Database. The data is, what you define in the command text. &lt;br /&gt;
Now, we attach the command object to the DataAdapter:&lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;da.&lt;span style=&quot;color: #000000;&quot;&gt;SelectCommand&lt;/span&gt; = SqlSelectCmd;&lt;/div&gt;&lt;br /&gt;
Well, there is one step required to finish our lesson. We fill our created DataTable with data. &lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtAccount&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;
Thats it!&lt;br /&gt;
&lt;br /&gt;
The fill-method of the DataAdapter needs a DataTable or a DataSet (or another tablecomponent) that will be filled after you invoke the method. Notice that the fill method opens and closes the connection automaticaly (we attached the connection to the command object). Feel free to open a connection manually, but don&#039;t forget to close it after the transaction! In case, you open a connection manually, the fill-mehtod will not manage the connection automatically. In fact it can be usefull to open and close a connection manually. Look at the code below:&lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtAccount&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtArticle&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtShop&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;
Well, this will  open and close the connection three times!!! &lt;br /&gt;
&lt;br /&gt;
In this case, we open and close the conneciton one times:&lt;br /&gt;
&lt;div class=&quot;csharp&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;Try&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; SqlConn.&lt;span style=&quot;color: #000000;&quot;&gt;Open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&amp;#160; &amp;#160; da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtAccount&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;#160; &amp;#160; da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtArticle&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;#160; &amp;#160; da.&lt;span style=&quot;color: #000000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;dtShop&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;SqlConn.&lt;span style=&quot;color: #000000;&quot;&gt;Close&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;SqlException SqlEx&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160;&lt;span style=&quot;color: #522994;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #000000;&quot;&gt;Windows&lt;/span&gt;.&lt;span style=&quot;color: #000000;&quot;&gt;Forms&lt;/span&gt;.&lt;span style=&quot;color: #000000;&quot;&gt;MessageBox&lt;/span&gt;.&lt;span style=&quot;color: #000000;&quot;&gt;Show&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #840000;&quot;&gt;&amp;quot;A problem occurs while connecting to the server!&amp;quot;&lt;/span&gt; + SqlEx.&lt;span style=&quot;color: #000000;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Why so, I have already told you - becouse if you open a connection manually, the fill method doesen&#039;t care about the connections anymore (only for the current usage). Becouse there can be caused lots of errors while connecting to the server, you should place the commands in a try-catch block. &lt;br /&gt;
 
    </content:encoded>

    <pubDate>Wed, 04 Apr 2007 10:42:03 +0000</pubDate>
    <guid isPermaLink="false">http://www.script0r.ndev.de/index.php?/archives/13-guid.html</guid>
    
</item>

</channel>
</rss>