//1、使用sqlbulkcopy.writetoserver(datatable)
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = "Table_1";//数据耐液库中的表名
sqlbulkcopy.WriteToServer(dataset.Tables[0]);
//2、使用sqlcommand.executenonquery()方法插入
foreach (DataRow datarow in datatable.Rows)
{
string sql = "INSERT INTO [Table_1]
([CompanyName],[CompanyCode],[Address],[Owner],[Memo])" +
"VALUES('" + datarow["CompanyName"].ToString() + "'" +
",'" + datarow["CompanyCode"].ToString() + "'" +
",'" + datarow["Address"].ToString() + "'" +
",'" 租仿+ datarow["Owner"].ToString() + "'" +
",'" + datarow["Memo"].ToString() + "')";
using (SqlConnection sqlconn = new SqlConnection(connectionString))
{
sqlconn.Open();
SqlCommand sqlcommand = new SqlCommand(sql, 弊亩纤sqlconn);
sqlcommand.ExecuteNonQuery();
sqlconn.Close();
}
}
使用sqldataadapter.update(dataset,tablename);
SqlCommand insertcommand = new SqlCommand("INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],[Memo])" +
"VALUES(@CompanyName, @CompanyCode,@Address,@Owner,@Memo)",new SqlConnection(connectionString));
insertcommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 50, "CompanyName");
insertcommand.Parameters.Add("@CompanyCode", SqlDbType.NChar, 25, "CompanyCode");
insertcommand.Parameters.Add("@Address", SqlDbType.NChar, 255, "Address");
insertcommand.Parameters.Add("@Owner", SqlDbType.NChar, 25, "Owner");
insertcommand.Parameters.Add("@Memo", SqlDbType.NChar, 255, "Memo");
sqldataadapter.InsertCommand = insertcommand;
sqldataadapter.Update(dataset, "Table_1");
使用sqlbulkcopy.writetoserver(datatable)
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = "Table_1";//数据库中的表名
sqlbulkcopy.WriteToServer(dataset.Tables[0]);
可以在sql里边建表变量类型,神并再做为存储过程的参数,就可以传了,也可以用逗分或其它符号隔开每行的数据改谨,每列传一个参数,再从核瞎基存储过程里边转换成表
for循环DataTable的Rows.Count,实体类模正接受DataTable.Rows[i]["Column"],调用BLL添加方改含法旦歼悔。