string sql = "SELECT * FROM sourceView";
using (SqlConnection conn = new SqlConnection(dsn))//source dsn
using (SqlCommand cmd = new SqlCommand(sql, conn))
using (SqlBulkCopy copy = new SqlBulkCopy(dsn2))//destination & source dsn are the same
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
copy.DestinationTableName = "destinationTable";
//have to map the columns because the tables are not an exact match
copy.ColumnMappings.Add("fieldname", "samefieldButSlightlyDifferentName");
copy.ColumnMappings.Add("fieldname2", "samefieldButSlightlyDifferentName2");
copy.WriteToServer(rdr);
conn.Close();
}
A couple things to note. I copied code from the last time I did this. Reading my comment I'm not sure I actually needed to map out the columns, because from my view to the destination the schemas matched up. Last time I did this however, there were a few more columns in the destination table, hence my comment about the not matching up. Second, I got this error early on: SqlBulkCopy: the locale id '0' of the source column '
2 comments:
If it's for an one-time thing, can I just use an "INSERT ... SELECT" sql statement?
http://www.techonthenet.com/sql/insert.php
Interesting question. I didn't know, but I looked it up. According to the msdn "There are other ways to load data into a SQL Server table (INSERT statements, for example), but SqlBulkCopy offers a significant performance advantage over them." (http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx) This "significant performance advantage" is not exactly qualified by this article. Other articles out there (http://davidhayden.com/blog/dave/archive/2006/03/08/2877.aspx and http://dotnettipoftheday.org/tips/sqlbulkcopy.aspx) also imply that sqlbulkcopy class is faster.
Post a Comment