access最快速的批量导入SQL的方法 使用SqlBulkCopy

access最快速的批量导入SQL的方法 使用SqlBulkCopy
2008-11-22 23:51

string filename = DateTime.Now.ToString("yyyyMMdd") + ".mdb";
        string tablename = DateTime.Now.ToString("yyyyMMdd");

        if (File.Exists(Server.MapPath("/tempmdb/" + filename)))
        {
            //查询,如果此表存在,就表示已经添加过了, 不允许再添加
            if (db.counts("select count(id) from sysobjects where name='" + tablename + "'") > 0)
            {
                cm.msg("今天的数据表已经存在,您可能已经导入过了.请检查!!");
                Response.End();
            }
            DateTime d = DateTime.Now;

            //读取源文件


            db.ExeSql("select * into [" + tablename + "] from PosSrcTable");
            //读数据,写SQL
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Server.MapPath("/tempmdb/" + filename) + ";");


            DataTable dts = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter("select sss_date,custom_no,txn_type from ls", conn);
            da.Fill(dts);
            string connstr = "server=(local);database=fasfdas;uid=sa;pwd=adfdasmin";
          
           SqlBulkCopy bcp = new SqlBulkCopy(connstr);
            bcp.DestinationTableName = "[" + tablename + "]";
            bcp.WriteToServer(dts);
            //删除原来的表
            File.Delete(Server.MapPath("/tempmdb/" + filename));
            string totalitem = dts.Rows.Count.ToString();
            dts.Clear();
            dts.Dispose();
            TimeSpan ts = DateTime.Now.Subtract(d);
          
            Response.Write("共导入" + totalitem + "条数据! 用时 " + ts.Seconds.ToString()+"秒 ");
            Response.End();

14万条数据 使用时间6秒.

你可能感兴趣的:(.NET)