zoukankan      html  css  js  c++  java
  • (转)把Excel表中的数据导入数据库(存储过程、数据库作业)

    转自:http://www.cnblogs.com/David-weihw/archive/2007/01/24/629213.html
    个人认为不错,可以在工作中使用,所以转来保存使用.

     把Excel表中的数据导入数据库

    【需求】
              在实现竞价网站时,需要把Excel表中的数据周期地有条件地导入到网站位表中。这里结合数据库作业实现。

    【步骤】

    1、生成一个Excel表
       工作表名称:website
       包含的列:spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
     
       部分截图:
     

    2、存储过程
     
    --1、涉及到的表:WL_SitePosition(网站位表),WL_ComSite(竞价网站表),Excel表(需要物理路径)
    --2、功能:若网站位表不存在记录,则从Excel表中导入;反之,把网站位表中的记录导入到竞价网站表,
    --同时,清空网站位表,之后再把Excel表中的数据导入到表WL_SitePosition;


    CREATE PROCEDURE wl_JOB_WebsiteAuction AS

    Declare @err1 int
    Declare @err2 int
    Declare @err3 int
    Declare @err4 int
    Declare @err5 int
    Declare @err6 int

    SET @err1 = 0
    SET @err2 = 0
    SET @err3 = 0
    SET @err4 = 0
    SET @err5 = 0
    SET @err6 = 0

    SET NOCOUNT ON
    --设置事务隔离级别
    --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    --OLE/DB provider  不支持隔离层
    Begin Tran --开始事务

      --SET XACT_ABORT ON --

       if (select count(*) from WL_SitePosition)>0
     begin
      --把数据从网站位表中导入到竞价网站表  
      TRUNCATE TABLE WL_ComSite --清空竞价网站表
      SET @err1 = @@error
      
      --把数据从网站位表中导入到竞价网站表中
      insert into WL_ComSite(cSiteName,cSiteUrl,cSiteVDT,spID,buyerID)
      select siteName,siteUrl,DATEADD(day,7,spEAucDT) as cSiteVDT,spID,buyerID
      from WL_SitePosition order by spID
      SET @err2 = @@error

      TRUNCATE TABLE WL_SitePosition--清空网站位表
      SET @err3 = @@error
      
      --把数据从Excel表导入到网站位表中
      insert into WL_SitePosition(spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl)
      SELECT spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
      FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="D:\work\我傲\竞拍设计\webSitePosition_Data.xls";Extended Properties="Excel 8.0";Persist Security Info=False')...[website$]
      SET @err4 = @@error
         
     end
       else
     begin
      
      
      --把数据从Excel表导入到网站位表中
      insert into WL_SitePosition(spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl)
      SELECT spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
      FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="D:\work\我傲\竞拍设计\webSitePosition_Data.xls";Extended Properties="Excel 8.0";Persist Security Info=False')...[website$]
      SET @err5 = @@error
      
      --把数据从网站位表中导入到竞价网站表中
      insert into WL_ComSite(cSiteName,cSiteUrl,cSiteVDT,spID,buyerID)
      select siteName,siteUrl,DATEADD(day,7,spEAucDT) as cSiteVDT,spID,buyerID
      from WL_SitePosition order by spID
      SET @err6 = @@error

     end

       if @err1=0 and @err2=0 and @err3=0 and @err4=0 and @err5=0 and @err6=0

     COMMIT TRAN
       else
     ROLLBACK TRAN

    SET NOCOUNT OFF
    GO

    3、创建数据库作业的截图

       






    注意:数据库服务器要启动 SQL Server代理

    【注意】
                 1、Excel表使用绝对路径
                  2、工作表的名称要一致,这里使用的是website

  • 相关阅读:
    Repeater OnItemCommand 失效
    继承类时的 this 和 base
    想当然造成的的错误
    排序算法二叉树
    href 一个正则表达式的解析 ? 号解析
    给父窗口添加新的代码
    ValidationSummary 控件
    交叉表 学习
    定向思维 C# datediff
    cookie 问题
  • 原文地址:https://www.cnblogs.com/dagon007/p/632907.html
Copyright © 2011-2022 走看看