protected void Button1_Click(object sender, EventArgs e)
{
string sString;
string nString;
SqlConnection sConn = new SqlConnection();
SqlConnection nConn = new SqlConnection();
SqlCommand sCom = new SqlCommand();
SqlDataReader sRead;
sString = ConfigurationManager.ConnectionStrings["sDB"].ConnectionString;
nString = ConfigurationManager.ConnectionStrings["nDB"].ConnectionString;
sConn.ConnectionString = sString;
sCom.Connection = sConn;
sCom.CommandText = "Select top 500 * from " + TextBox1.Text.Trim();
sCom.CommandType = CommandType.Text;
sCom.Connection.Open();
SqlBulkCopy nBulk;
nBulk = new SqlBulkCopy(nString, SqlBulkCopyOptions.UseInternalTransaction);
nBulk.DestinationTableName = TextBox1.Text.Trim();
//nBulk.ColumnMappings.Add("x", "q");
//nBulk.ColumnMappings.Add("a", "m");
//nBulk.ColumnMappings.Add("b", "x");
//nBulk.ColumnMappings.Add("c", "y");
//nBulk.ColumnMappings.Add("d", "z");
nBulk.BulkCopyTimeout = 500000000;
nBulk.SqlRowsCopied +=new SqlRowsCopiedEventHandler(OnRowsCopied);
nBulk.NotifyAfter = 1000;
sRead = sCom.ExecuteReader();
try
{
nBulk.WriteToServer(sRead);
}
catch (Exception ex)
{
Response.Write(ex.Message);
throw;
}
finally
{
sRead.Close();
}
}
TextBox1.Text.Trim() 用来输入表名。