UpdateDataset(datasset,new string[] {this.H_TableName ,this.D_TableName},DataCon.conString);public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName) { if( insertCommand == null ) throw new ArgumentNullException( "insertCommand" ); if( deleteCommand == null ) throw new ArgumentNullException( "deleteCommand" ); if( updateCommand == null ) throw new ArgumentNullException( "updateCommand" ); if( tableName == null || tableName.Length == 0 ) throw new ArgumentNullException( "tableName" ); // Create a SqlDataAdapter, and dispose of it after we are done using (SqlDataAdapter dataAdapter = new SqlDataAdapter()) { // Set the data adapter commands dataAdapter.UpdateCommand = updateCommand; dataAdapter.InsertCommand = insertCommand; dataAdapter.DeleteCommand = deleteCommand; // Update the dataset changes in the data source dataAdapter.Update (dataSet, tableName); // Commit all the changes made to the DataSet dataSet.AcceptChanges(); } }
依据以上需求我觉得使用asp.net 2.0中的SqlBulkCopy类批量复制数据private static void PerformBulkCopyXMLDataSource() { string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true"; DataSet ds = new DataSet(); DataTable sourceData = new DataTable(); ds.ReadXml(@"C:Products.xml"); sourceData = ds.Tables[0]; // 目的 using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { // 打开连接 destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString)) { // 列映射 bulkCopy.ColumnMappings.Add("productID", "ProductID"); bulkCopy.ColumnMappings.Add("productName", "Name");
再一行一行的insert到表中for each(DataRow dr in ds)
{
string sql = “insert into table1 values (” + “‘” + dr[“字段名1”]+”’,‘“ + dr[“字段名2”]+.................
InsertIntoTable(sql)
}private void InsertIntoTable(string sql)
{
'执行插入数据库表的函数。
}
整个的提交不用一个一个的insert没试过,excel文件不是可以直接导入表吗?数据库有这个功能吧!具体用代码怎么写,我也不清楚。
6、创建SQL*Loader输入数据所需要的文件,均保存到C:\,用记事本编辑: 控制文件:input.ctl,内容如下: load data --1、控制文件标识 infile 'test.txt' --2、要输入的数据文件名为test.txt append into table test--3、向表test中追加记录 fields terminated by X'09'--4、字段终止于X'09',是一个制表符(TAB) (id,username,password,sj) -----定义列对应顺序 a、insert,为缺省方式,在数据装载开始时要求表为空 b、append,在表中追加新记录 c、replace,删除旧记录,替换成新装载的记录 d、truncate,同上 7、在DOS窗口下使用SQL*Loader命令实现数据的输入 C:\>sqlldr userid=system/manager control=input.ctl 默认日志文件名为:input.log 默认坏记录文件为:input.bad 如果是远程对数据库进行导入操作,则输入字符串应改为: C:\>sqlldr userid=system/manager@serviceName_192.168.1.248 control=input.ctl 8、连接到SQL*Plus中,查看是否成功输入,可比较input.log与原test.xls文件,查看数据是否全部导入,是否导入成功.
2.使用适配器得到一个该表的目标dataset
3.将你的源dataset合并到那个目标dataset里去
4.目标dataset.AcceptChange()
然后 给dataadapter创建更新语句,然后使用dataset.marge方法合并两个dataset然后使用dataaadpter的update方法应该就可以了
直接调用 sql server bcp工具
直接把excel数据批量导入到数据库对应的表中了
{
if( insertCommand == null ) throw new ArgumentNullException( "insertCommand" );
if( deleteCommand == null ) throw new ArgumentNullException( "deleteCommand" );
if( updateCommand == null ) throw new ArgumentNullException( "updateCommand" );
if( tableName == null || tableName.Length == 0 ) throw new ArgumentNullException( "tableName" ); // Create a SqlDataAdapter, and dispose of it after we are done
using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
{
// Set the data adapter commands
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.InsertCommand = insertCommand;
dataAdapter.DeleteCommand = deleteCommand; // Update the dataset changes in the data source
dataAdapter.Update (dataSet, tableName); // Commit all the changes made to the DataSet
dataSet.AcceptChanges();
}
}
{
string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true"; DataSet ds = new DataSet();
DataTable sourceData = new DataTable();
ds.ReadXml(@"C:Products.xml"); sourceData = ds.Tables[0]; // 目的
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
// 打开连接
destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
// 列映射
bulkCopy.ColumnMappings.Add("productID", "ProductID");
bulkCopy.ColumnMappings.Add("productName", "Name");
bulkCopy.DestinationTableName = "Products_TopSelling";
bulkCopy.WriteToServer(sourceData);
}
}
}首先把XML文件读进DataTable,然后再使用SqlBulkCopy类的WriteToServer方法。 因为目的表示是Products_TopSelling,所以我们必须执行列映射。
当然咯,你用的不是XML,这是我自己的例子