<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scott Means &#187; .Net</title>
	<atom:link href="http://smeans.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://smeans.com</link>
	<description>Ripping the envelope of software development, one technology at a time.</description>
	<lastBuildDate>Tue, 06 Oct 2009 13:47:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>My MySQL tool is on softpedia.com.</title>
		<link>http://smeans.com/2009/06/18/my-mysql-tool-is-on-softpediacom/</link>
		<comments>http://smeans.com/2009/06/18/my-mysql-tool-is-on-softpediacom/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 20:06:21 +0000</pubDate>
		<dc:creator>smeans</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://smeans.com/?p=184</guid>
		<description><![CDATA[I just got the notice today. I wrote this bulk row insert tool for MySQL that is designed to run over the Internet. It&#8217;s for really large data sets being loaded into &#8230; ahem &#8230; inexpensive hosted MySQL databases. Check out the mysqlxfer softpedia page.
]]></description>
			<content:encoded><![CDATA[<p>I just got the notice today. I wrote this bulk row insert tool for MySQL that is designed to run over the Internet. It&#8217;s for really large data sets being loaded into &#8230; ahem &#8230; <em>inexpensive</em> hosted MySQL databases. Check out the <a href="http://www.softpedia.com/get/Internet/Servers/Database-Utils/mysqlxfer.shtml">mysqlxfer softpedia page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://smeans.com/2009/06/18/my-mysql-tool-is-on-softpediacom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows hosting with GoDaddy is a royal PITA.</title>
		<link>http://smeans.com/2008/05/06/windows-hosting-with-godaddy-is-a-royal-pita/</link>
		<comments>http://smeans.com/2008/05/06/windows-hosting-with-godaddy-is-a-royal-pita/#comments</comments>
		<pubDate>Tue, 06 May 2008 18:42:11 +0000</pubDate>
		<dc:creator>smeans</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://smeans.com/2008/05/06/windows-hosting-with-godaddy-is-a-royal-pita/</guid>
		<description><![CDATA[At least the shared stuff is. I&#8217;ve spent at least four hours today trying to get my SQL 2005 database configured on their server. Since they don&#8217;t allow remote access through SQL Server Management Studio, I&#8217;ve been forced to use their not-so-powerful web-based administration tool. Compounding that is the fact that I used to have [...]]]></description>
			<content:encoded><![CDATA[<p>At least the shared stuff is. I&#8217;ve spent at least four hours today trying to get my SQL 2005 database configured on their server. Since they don&#8217;t allow remote access through SQL Server Management Studio, I&#8217;ve been forced to use their not-so-powerful web-based administration tool. Compounding that is the fact that I used to have references between two different databases on my local server that I now need to merge into a single monolithic database, I&#8217;m not a happy camper.</p>
<p>As part of this process I&#8217;ve had to populate a bunch of tables with application-specific values. After wrestling with the crappy CSV import function that GoDaddy gave me I decided to write my own little tool to generate SQL INSERT statements for the data in my tables. Since WordPress won&#8217;t let me upload it, here&#8217;s the code:</p>
<pre>
<code>/*
 * Created by SharpDevelop.
 * User: smeans
 * Date: 5/6/2008
 * Time: 11:58 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace sql2insert
{
  class sql2insert
  {
    public static void Main(string[] args)
    {
      foreach (string dsn in args) {
        Console.WriteLine("writing files to " + System.Environment.CurrentDirectory);

        using (SqlConnection cn = new SqlConnection(args[0])) {
          cn.Open();

          DataTable dt = cn.GetSchema("Tables");

          foreach (DataRow row in dt.Rows) {
            for (int i = 0; i &lt; dt.Columns.Count; i++) {
              if (((string)row[3]).Equals("BASE TABLE")) {
                dumpTable(cn, (string)row[2]);
              }
            }
          }
        }
      }
    }

   static void dumpTable(SqlConnection cn, string tableName) {
     SqlCommand cmd = new SqlCommand(String.Format("SELECT * FROM [{0}]", tableName), cn);

     SqlDataReader sdr = cmd.ExecuteReader();

     if (sdr.HasRows) {
       Console.WriteLine("writing data for table " + tableName);

       using (TextWriter tw = new StreamWriter(String.Format("{0}.sql", tableName))) {
         tw.WriteLine(String.Format("SET IDENTITY_INSERT [{0}] ON", tableName));
         tw.WriteLine("GO\\r\\n");

         StringBuilder sbCols = new StringBuilder();

         for (int i = 0; i &lt; sdr.FieldCount; i++) {
          switch (sdr.GetFieldType(i).ToString()) {
             case "System.Byte[]": {
             } break;
             default: {
              if (sbCols.Length &gt; 0) {
                sbCols.Append(", ");
              }

              sbCols.Append(String.Format("[{0}]", sdr.GetName(i)));
             } break;
          }
         }

         tw.WriteLine();

         while (sdr.Read()) {
           tw.Write(String.Format("INSERT INTO [{0}] ({1}) VALUES (", tableName, sbCols.ToString()));

           for (int i = 0; i &lt; sdr.FieldCount; i++) {
            if (sdr.IsDBNull(i)) {
               if (i &gt; 0) {
                tw.Write(',');
               }

               tw.Write("null");
            } else {
             switch (sdr.GetFieldType(i).ToString()) {
             case "System.Int32": {
               if (i &gt; 0) {
                tw.Write(',');
               }

               tw.Write(sdr[i].ToString());
             } break;

             case "System.Decimal": {
               if (i &gt; 0) {
                tw.Write(',');
               }

               tw.Write(sdr[i].ToString());
             } break;

             case "System.Byte[]": {
             } break;

             case "System.DateTime": {
               if (i &gt; 0) {
                tw.Write(',');
               }

               tw.Write(String.Format("'{0}'", ((DateTime)sdr[i]).ToString("M/d/yyyy h:m:s tt")));
             } break;

             default: {
               if (i &gt; 0) {
                tw.Write(',');
               }

              tw.Write(String.Format("'{0}'", sdr[i].ToString().Replace("\\'", "\\'\\'")));
             } break;
             }
           }
           }

           tw.WriteLine(")\\r\\nGO");
         }

         tw.WriteLine(String.Format("SET IDENTITY_INSERT [{0}] OFF", tableName));
         tw.WriteLine("GO\\r\\n");

         tw.Close();
       }
     }

     sdr.Close();
   }
}
}</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://smeans.com/2008/05/06/windows-hosting-with-godaddy-is-a-royal-pita/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with SqlCommand.ExecuteXmlReader()</title>
		<link>http://smeans.com/2008/03/12/fun-with-sqlcommandexecutexmlreader/</link>
		<comments>http://smeans.com/2008/03/12/fun-with-sqlcommandexecutexmlreader/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 15:07:52 +0000</pubDate>
		<dc:creator>smeans</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://smeans.com/2008/03/12/fun-with-sqlcommandexecutexmlreader/</guid>
		<description><![CDATA[So I&#8217;m coding along this morning, minding my own business, when I run up against a really annoying problem with the XML support in the .Net SQL support classes. What I was trying to do was fetch some values from a support table as a simple XML document with an element for each row. The [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m coding along this morning, minding my own business, when I run up against a really annoying problem with the XML support in the .Net SQL support classes. What I was trying to do was fetch some values from a support table as a simple XML document with an element for each row. The SQL <code>FOR XML AUTO</code> clause was just the ticket, so I wrote the following code:</p>
<pre>
<code>SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM "
  + "supportTable AS valueName FOR XML AUTO";

XmlReader res = cmd.ExecuteXmlReader();

XmlDocument doc = new XmlDocument();
doc.Load(res);</code>
</pre>
<p>but when I tried to run the code I got the following error on the doc.Load() call:</p>
<pre><code>This document already has a DocumentElement node.</code></pre>
<p>It really had me irritated, because I&#8217;d already used the exact same code in another part of my application without a hitch. So after some research, it turns out that the problem (which is obvious in retrospect) is that if multiple rows are returned, the resulting XML is not a valid document, but a document fragment. Since there is no top-level element, the Load() method chokes when it encounters the second row. Not good.</p>
<p>I found several workarounds on the web, but none that I really cared for. So I resorted to some MS SQL funny business that I&#8217;ve used in the past that did the trick. The problem here is that there is no single top-level element to serve as a root element. One possible approach is to abandon the <code>FOR XML AUTO</code> in favor of <code>FOR XML EXPLICIT</code>, but the explicit support is so incredibly complex and unusable that I&#8217;d rather cut my own foot off with a rusty tin can lid. So, to save my foot, I resorted to some fun with joins.</p>
<p>The <code>FOR XML AUTO</code> clause will actually create nested elements in the case of joins between tables, so to create a single top-level element for my support table document I created a new table in my DB called <code>dummy</code>. It has a single column (<code>dummy</code>) and a single row with a single value (<code>dummy</code>). The single row part is important, because by changing the SQL statement above slightly, I end up with the nice, valid XML document I want:</p>
<pre>
<code>SELECT * FROM dummy AS rootElementName,
  supportTable AS valueName FOR XML AUTO</code>
</pre>
<p>Voila! Now my code works and I can move on to more interesting pursuits, like the development of a <a href="http://en.wikipedia.org/wiki/Comet_(programming)">Comet</a> service for the masses. Good stuff!</p>
]]></content:encoded>
			<wfw:commentRss>http://smeans.com/2008/03/12/fun-with-sqlcommandexecutexmlreader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
