Skip to main content

Posts

Showing posts from September, 2010

Save DataTable to SQL Server

A couple of times now, I've needed to save in-memory ADO.NET DataTables straight to SQL Server as new tables. Its handy sometimes, if you're importing data from other sources. Nick Tompson had posted some code on the social.msdn forum to do this, and here I've stripped out the features I didn't need and adapted it to use StringBuilders: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; // Save a DataTable to a SQLServer Table // Based on Nick Tompsons code from // http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/4929a0a8-0137-45f6-86e8-d11e220048c3/ // Removed 'make from Schema' options to simplify, and used StringBuilders. public class SqlTableCreator { private SqlConnection _connection; public SqlConnection Connection { get { return _connection; } set { _connection