關於利用opendatasource 讀取excel文件中的數據問題以及更新數據庫中對應數據的問題

<strong>前言:</strong>
   <strong> 由於下面是根據別人的提問解決過程的一個回顧總結,若碰到類似問題,按照下面步驟不能解決,並找到了解決辦法的朋友,歡迎補充!</strong>
/*
    操作系統:win7 32位處理器
    Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
	Oct 14 2005 00:33:37 
	Copyright (c) 1988-2005 Microsoft Corporation
	Developer Edition on Windows NT 6.1 (Build 7601: Service Pack 1)

       excel 爲2010
*/

--1.<strong>設置sqlserver configuration manager->功能的外圍應用配置器開啓 openrowset和opendatasource功能</strong>

--2.<strong>設置參數值爲非空(分別爲允許進程和動態參數)</strong>
USE [master]    
GO    
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1    
GO    
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1    
GO    

或者
<strong>management studio->服務器對象->鏈接服務器->訪問接口</strong>


select * from OPENDATASOURCE(     --openrowset()同樣也可以用來訪問本機中excel中的數據
         'Microsoft.Ace.OleDb.12.0',  --EXCEL版本爲2010及以上版本可用,如果是2007及以下版本可用microsoft.jet.oled.4.0
         'Extended Properties="Excel 12.0;HDR=YES;IMEX=1";Data Source="E:\test.xlsx"'  
         )...[test$]
或者
select * from opendatasource('Microsoft.ace.oledb.12.0', 'Excel 8.0;Database=e:\test.xlsx')...[test$]   


--<strong>使用opendatasource更新數據庫中的表</strong>
update t1 set name=a.sname
from (select * from    OPENDATASOURCE(  
         'Microsoft.Ace.OleDb.12.0',  
         'Extended Properties="Excel 12.0;HDR=YES;IMEX=1";Data Source="E:\test.xlsx"'  
         )...[test$]  ) a where t1.id=a.id


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章