DataTable dt = new DataTable();
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add("zzq1", 19);
dt.Rows.Add("zzq2", 20);
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
{
//指定目标数据库的表名
bcp.DestinationTableName = "UserInfo"; //建立数据源表字段和目标表中的列之间的映射
bcp.ColumnMappings.Add("UserName", "UserName");
bcp.ColumnMappings.Add("Age", "Age"); //写入数据库表 dt 是数据源DataTable
bcp.WriteToServer(dt);
//关闭SqlBulkCopy实例
bcp.Close();
} //using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
//{
// string sql = "Delete [Test].[dbo].[UserInfo]";
// using (SqlCommand com = new SqlCommand(sql, conn))
// {
// conn.Open();
// object obj = com.ExecuteScalar();
// }
//}