//????????????????
string strSql = string.Format("if object_id('{0}') is null create table {0}("?? sheetName);
foreach (System.Data.DataColumn c in ds.Tables[0].Columns)
{
strSql += string.Format("[{0}] varchar(255)??"?? c.ColumnName);
}
strSql = strSql.Trim('??') + ")";
using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
{
sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;
command.ExecuteNonQuery();
sqlconn.Close();
}
//??bcp????????
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//??δ????????
bcp.NotifyAfter = 100;//?????????????
bcp.DestinationTableName = sheetName;//????
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
//???????
void bcp_SqlRowsCopied(object sender?? System.Data.SqlClient.SqlRowsCopiedEventArgs e)
{
this.Text = e.RowsCopied.ToString();
this.Update();
}
}
}
?????????TransferData?????????????????????????????????????oledb?????excel?????????????ColumnMappings???????????Σ?????Ч???????????????sqlserver??dts?????Ч?????