<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Imports/Exports</title>
        <link>http://weblogs.sqlteam.com/jeffs/category/249.aspx</link>
        <description>Importing and Exporting data to and from SQL Server.</description>
        <language>en-US</language>
        <copyright>Jeff Smith</copyright>
        <managingEditor>smith_jeffreyt@yahoo.com</managingEditor>
        <generator>Subtext Version 1.9.4.0</generator>
        <item>
            <title>Exporting data to a remote server with SQL Express</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/02/27/60539.aspx</link>
            <description>I recently helped a friend out who only had access to &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796&amp;amp;displaylang=en" target="_blank"&gt;SQL Server Management Studio Express&lt;/a&gt;, and he needed to copy a database locally from his PC to his remote web hosting company.  Normally, the process is a simple backup/restore, but his hosting company does not allow restoring databases.  Luckily, however, the company does allow direct access to his hosted database via client tools such as SSMS.  Unluckily, SSMS Express does not include any tools that allow you to export data to a remote server.&lt;br /&gt;
&lt;br /&gt;
So, here's what we did:  First, we scripted out the entire database, including all tables, indexes, constraints, views, functions, procs, etc, using SSMS Express.  This took some time to execute, but it was easy to do.&lt;br /&gt;
&lt;br /&gt;
Then, we ran that script remotely on the hosted database to create the environment.  Easy enough.&lt;br /&gt;
&lt;br /&gt;
The final step was potentially the hardest part: Now we had to copy all of the data in all of the tables from the local database to the remote database.  I thought about generating INSERT scripts, or even using a tool like MS Access (via linked tables) to get this done, but I decided against them because the INSERT script would be huge (there is a lot of data we are inserting) and any of these methods would require loading data in the exact order needed to satisfy FK constraints.  In addition, there was the issue of IDENTITY values and handling that.  All could be done, of course, but we were pressed for time and we needed something simple and efficient and that we could easily run again and again as needed.&lt;br /&gt;
&lt;br /&gt;
Then, I remembered my little friend &lt;a target="_blank" href="http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server"&gt;SQLBulkCopy&lt;/a&gt;, the really handy .NET 2.0 class that facilitates quickly copying data to a SQL Server database.  I had forgotten it includes options to allow for identity values to be specified when bulk inserting, and for FK constraints to be be ignored as well.  So, the order that the tables are uploaded in doesn't matter, and identity values are handled fine as well.&lt;br /&gt;
&lt;br /&gt;
A few minutes later, a simple C# console app was written and we were ready to roll:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="background: white none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: Verdana; font-size: 8pt; line-height: 10pt; color: black; margin-left: 40px;"&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt; System;&lt;br /&gt;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Data;&lt;br /&gt;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Data.SqlClient;&lt;br /&gt;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Text;&lt;br /&gt;
 &lt;br /&gt;
&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; CopyData&lt;br /&gt;
{&lt;br /&gt;
    &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&lt;br /&gt;
    {&lt;br /&gt;
        &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;
        {&lt;br /&gt;
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; Src=&lt;span style="color: rgb(163, 21, 21);"&gt;"-- source connection string here--"&lt;/span&gt;;&lt;br /&gt;
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; Dest = &lt;span style="color: rgb(163, 21, 21);"&gt;"-- dest connection string here --"&lt;/span&gt;;&lt;br /&gt;
            &lt;span style="color: blue;"&gt;int&lt;/span&gt; BatchSize = 500;&lt;br /&gt;
            &lt;span style="color: blue;"&gt;int&lt;/span&gt; NotifyAfter = 500;&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlBulkCopy&lt;/span&gt; c = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlBulkCopy&lt;/span&gt;(Dest, &lt;span style="color: rgb(43, 145, 175);"&gt;SqlBulkCopyOptions&lt;/span&gt;.KeepIdentity | &lt;span style="color: rgb(43, 145, 175);"&gt;SqlBulkCopyOptions&lt;/span&gt;.TableLock);&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt; srcConn = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt;(Src);&lt;br /&gt;
            srcConn.Open();&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlCommand&lt;/span&gt; cm = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"select * from sysobjects where type='u'"&lt;/span&gt;);&lt;br /&gt;
            cm.Connection = srcConn;&lt;br /&gt;
            cm.CommandType = &lt;span style="color: rgb(43, 145, 175);"&gt;CommandType&lt;/span&gt;.Text;&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt; AllTables = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt;();&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlDataAdapter&lt;/span&gt; a = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlDataAdapter&lt;/span&gt;(cm);&lt;br /&gt;
            a.Fill(AllTables);&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlDataReader&lt;/span&gt; dr;&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: blue;"&gt;int&lt;/span&gt; n = 0;&lt;br /&gt;
            &lt;span style="color: blue;"&gt;int&lt;/span&gt; tot = AllTables.Rows.Count;&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Found {0} Tables to copy."&lt;/span&gt;, tot);&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;);&lt;br /&gt;
 &lt;br /&gt;
            c.SqlRowsCopied += &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlRowsCopiedEventHandler&lt;/span&gt;(c_SqlRowsCopied);&lt;br /&gt;
            &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;DataRow&lt;/span&gt; r &lt;span style="color: blue;"&gt;in&lt;/span&gt; AllTables.Rows)&lt;br /&gt;
            {&lt;br /&gt;
                &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Coping table {0} of {1}: {2}"&lt;/span&gt;, ++n, tot, r[&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;]);&lt;br /&gt;
                cm.CommandText = &lt;span style="color: rgb(43, 145, 175);"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"select * from [{0}]"&lt;/span&gt;, r[&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;]);&lt;br /&gt;
                dr = cm.ExecuteReader();&lt;br /&gt;
                c.BatchSize = BatchSize;&lt;br /&gt;
                c.DestinationTableName = r[&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;].ToString();&lt;br /&gt;
                c.NotifyAfter = NotifyAfter;&lt;br /&gt;
                c.WriteToServer(dr);&lt;br /&gt;
                dr.Close();&lt;br /&gt;
            }&lt;br /&gt;
 &lt;br /&gt;
            srcConn.Close();&lt;br /&gt;
            c.Close();&lt;br /&gt;
 &lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;);&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Done .. press any key."&lt;/span&gt;);&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.ReadLine();&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; c_SqlRowsCopied(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;SqlRowsCopiedEventArgs&lt;/span&gt; e)&lt;br /&gt;
        {&lt;br /&gt;
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;" -- copied {0} rows ..."&lt;/span&gt;, e.RowsCopied);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
That's pretty much it... The app runs quicky, gives a nice progress report as it goes, and can easily be modified to use command line arguments or whatever else you need to make it simple and re-usable.&lt;br /&gt;
&lt;br /&gt;
So, if you are using SQL Express to develop an application, and you need to export that database to a hosted web server and a simple backup/restore is not an option, here's all you need to do:&lt;br /&gt;
&lt;br /&gt;
1) Create your destination database&lt;br /&gt;
2) Script out all objects in your source database&lt;br /&gt;
3) Run the script in your destination database&lt;br /&gt;
4) Create the C# console application shown above, enter in your connection strings in the first few lines where indicated, and run it.&lt;br /&gt;
&lt;br /&gt;
That's it!  Quick, simple and easy, and really not much harder than a backup/restore would be at all ... in fact, in some ways it is even easier than FTP'ing a backup to your hosted server and dealing with all that.&lt;br /&gt;
&lt;br /&gt;
If you are you using &lt;a target="_blank" href="http://www.microsoft.com/express/vwd/Default.aspx"&gt;Visual Web Developer&lt;/a&gt; to write your application, and therefore don't have the ability to write console applications like the one shown to do your export, you can do this by downloading &lt;a target="_blank" href="http://www.microsoft.com/express/vcsharp/"&gt;Visual C# Express&lt;/a&gt;.&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60539.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/02/27/60539.aspx</guid>
            <pubDate>Wed, 27 Feb 2008 20:04:45 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/jeffs/comments/60539.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/02/27/60539.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60539.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60539.aspx</trackback:ping>
        </item>
        <item>
            <title>Retrieving Identity Values When Inserting Multiple Rows</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2007/07/02/60246.aspx</link>
            <description>You're INSERTing multiple rows from an un-normalized import table.  Each row is assigned an identity primary key.  You know you can use scope_identity() to retrieve one identity at a time, but how do you retrieve a whole set of them? Are cursors the only answer?  As usual, it depends on having logical specifications and a good design. &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/07/02/60246.aspx"&gt;read more..&lt;/a&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60246.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2007/07/02/60246.aspx</guid>
            <pubDate>Mon, 02 Jul 2007 16:31:17 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/jeffs/comments/60246.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2007/07/02/60246.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60246.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60246.aspx</trackback:ping>
        </item>
        <item>
            <title>Using SQLBulkCopy to copy any .NET Object  to a SQL Server Table</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2007/05/22/sqlbulkcopy-any-dotnet-object.aspx</link>
            <description>In my &lt;a href="http://www.sqlteam.com/item.asp?ItemID=26941"&gt;SQLBulkCopy article&lt;/a&gt;, I mentioned that you can quickly copy data from anything that implements IDataReader to a SQL Server table using SQLBulkCopy (new in .NET 2.0).  In this &lt;a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=83181"&gt;SQLTeam forum post&lt;/a&gt;, &lt;a href="http://www.elsasoft.org/"&gt;Jesse Hersch&lt;/a&gt; (jezemine) tells us that SQLBulkCopy only actually uses 3 methods of the interface.  This means that it is very easy to quickly create your own custom class which implements this interface to bulk copy pretty much any type of object collection or array to a SQL Server table.  He even provides code for a simple abstract  SqlBulkCopyReader class that you can inherit from to implement only the necessary parts of the interface.&lt;br /&gt;
&lt;br /&gt;
So, if you need to persist large amounts of data from your .net applications directly to SQL Server, and that data is not stored in DataTables, be sure to consider this option.  Thanks, Jesse !&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60214.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2007/05/22/sqlbulkcopy-any-dotnet-object.aspx</guid>
            <pubDate>Tue, 22 May 2007 18:40:10 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/jeffs/comments/60214.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2007/05/22/sqlbulkcopy-any-dotnet-object.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60214.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60214.aspx</trackback:ping>
        </item>
        <item>
            <title>Using SQLBulkCopy to quickly transfer data from .NET to SQL Server</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2007/05/07/60196.aspx</link>
            <description>My latest article has just been posted over at &lt;a href="http://www.sqlteam.com/default.asp"&gt;SQLTeam.com&lt;/a&gt;: &lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;a style="font-weight: bold;" target="_blank" href="http://www.sqlteam.com/item.asp?ItemID=26941"&gt;Use SqlBulkCopy to Quickly Load Data from your Client to SQL Server &lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;.NET Framework 2.0 introduces a very handy new class in the System.Data.SqlClient namespace called SqlBulkCopy that makes it very easy and efficient to copy large amounts of data from your .NET applications to a SQL Server database. You can even use this class to write a short .NET application that can serve as a "middleman" to move data between database servers.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
If you ever need to move large amounts of data to SQL Server from a .NET application, &lt;a href="http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx"&gt;SQLBulkCopy&lt;/a&gt; is the way to go.  What's great about it is that you can use a populated DataTable as the source, or anything that implements &lt;a href="http://msdn2.microsoft.com/en-us/library/system.data.idatareader.aspx"&gt;IDataReader&lt;/a&gt;.  This means that you can use SQLBulkCopy to quickly move data from any OLEDB/ODBC datasource to SQL Server.&lt;br /&gt;
&lt;br /&gt;
Informal testing shows that it is about 4 times as fast as executing INSERT statements over and over when copying large amounts of data from a DataTable, and it certainly is much shorter and easier to write and maintain.&lt;br /&gt;
&lt;br /&gt;
I hope you find the article and the examples given useful.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.sqlteam.com/item.asp?ItemID=26941"&gt;Article link&lt;/a&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60196.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2007/05/07/60196.aspx</guid>
            <pubDate>Mon, 07 May 2007 13:29:46 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2007/05/07/60196.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60196.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60196.aspx</trackback:ping>
        </item>
    </channel>
</rss>