从 MS Access 批量导入并插入 Sql Server [关闭]
Posted
技术标签:
【中文标题】从 MS Access 批量导入并插入 Sql Server [关闭]【英文标题】:Bulk import from MS Access and Insert into Sql Server [closed] 【发布时间】:2011-04-29 05:52:13 【问题描述】:我想从 MS Access 数据库中读取记录并插入到 Sql server 数据库中,这个过程应该是批量插入。我正在使用 asp.net/vb.net
【问题讨论】:
【参考方案1】:首先从 Excel 表中读取数据
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/temp/") + "FileName.xlsx; Extended Properties=Excel 12.0;";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbDataAdapter adapter = factory.CreateDataAdapter();
DbCommand selectCommand = factory.CreateCommand();
selectCommand.CommandText = "SELECT ColumnNames FROM [Sheet1$]";
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
selectCommand.Connection = connection;
adapter.SelectCommand = selectCommand;
DataTable dtbl = new DataTable();
adapter.Fill(dtbl);
// Then use SQL Bulk query to insert those data
if (dtbl.Rows.Count > 0)
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection))
bulkCopy.ColumnMappings.Add("ColumnName", "ColumnName");
bulkCopy.ColumnMappings.Add("ColumnName", "ColumnName");
bulkCopy.DestinationTableName = "DBTableName";
bulkCopy.WriteToServer(dtblNew);
【讨论】:
从 Excel 读取数据的说明如何帮助人们从 Access/Jet/ACE 读取数据? @David,用该信息更新答案。谢谢你的亲密。【参考方案2】:private void Synchronize()
SqlConnection con = new SqlConnection("Database=DesktopNotifier;Server=192.168.1.100\\sql2008;User=common;Password=k25#ap;");
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM CustomerData", con);
DataSet ds = new DataSet();
adap.Fill(ds, "CustomerData");
DataTable dt = new DataTable();
dt = ds.Tables["CustomerData"];
foreach (DataRow dr in dt.Rows)
string File = dr["CustomerFile"].ToString();
string desc = dr["Description"].ToString();
string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=D:\\DesktopNotifier\\DesktopNotifier.accdb";
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
string dbcommand = "insert into CustomerData (CustomerFile, Description) VALUES ('" + File + "', '" + desc + "')";
OleDbCommand mscmd = new OleDbCommand(dbcommand, conn);
mscmd.ExecuteNonQuery();
private void Configuration_Load(object sender, EventArgs e)
LoadGridData();
LoadSettings();
【讨论】:
【参考方案3】:就我的两分钱...
使用这样的代码:
DataSet ds = new DataSet();
adap.Fill(ds, "CustomerData");
您应该知道数据适配器填充方法会将所有数据读取到内存中。因此,如果您有数以亿计的行...三思而后行。
【讨论】:
以上是关于从 MS Access 批量导入并插入 Sql Server [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 MS ACCESS 表执行 SQL Server 表的批量更新