通过异类查询(OpenDataSource)把外部数据(Excel,TXT)导入到SqlSe
无论用sqlcmd或者通过C#逻辑处理,或者还是直接在SqlServer里执行操作,个人总结大概不过可归总为这两种方式: 1,SSIS; 2,OLEDB驱动引擎。 SSIS实在也好用,现在这里先不去讨论。其中大多数人用的都是第二种方法,下面,本人也以第二种方法在SqlServer里导
无论用sqlcmd或者通过C#逻辑处理,或者还是直接在SqlServer里执行操作,个人总结大概不过可归总为这两种方式:1,SSIS;
2,OLEDB驱动引擎。
SSIS实在也好用,现在这里先不去讨论。其中大多数人用的都是第二种方法,下面,本人也以第二种方法在SqlServer里导入excel和txt格式文件的具体实例来说明怎样在项目里灵活通过OLEDB导入数据!
一,必要的环境说明
本实例用sql语句在SqlServer里面完成对数据的导入操作。在本实例开始之前,可能需要先安装AccessDatabase引擎包
(本人机器64位,安装的是32位的office组件,所以需要另外安装office驱动引擎包(此引擎包作用在于使得office系统文件与office应用程序之间进行数据传输) - 此步骤中需要先卸载32位office组件,否则会提示驱动安装不成功)
本人所用引擎包下载地址如下:
http://www.microsoft.com/zh-cn/download/details.aspx?id=13255
安装完成后,具体配置可以这里配,建议如非必要,默认就好。

二,代码准备 - 建数据库及存储过程
建数据库这里不多说,建立与导入数据相匹配的数据库而已。而为了项目拓展及方便使用管理,需要创建存储过程,目的是为了批量导入外部数据。
因为本例以excel和txt分别来做实例说明,所以,需要新建两个存储过程 - "sp_ReadXLSSource"和"sp_ReadSource"。代码分别如下:
存储过程:sp_ReadXLSSource
USE [CUSTOMS] GO /****** Object: StoredProcedure [dbo].[sp_ReadXLSSource] Script Date: 11/21/2014 3:01:39 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[sp_ReadXLSSource] @trgPath varchar(255), @trgTable varchar(255), @xlsFile varchar(255), @xlsSheet varchar(255), @xlsFields varchar(8000) AS declare @mySql varchar(8000) declare @myIntermediate varchar(8000) declare @Firstfield varchar(100) declare @FirstfieldValue varchar(100) --Lookup lastfield set @Firstfield = dbo.fnGetFirstField(@xlsFields) set @FirstfieldValue = RTRIM(LTRIM(SUBSTRING(@Firstfield,2,CHARINDEX(']',@Firstfield,0)-2))) --PRINT @FirstfieldValue --Drop table if exists set @myIntermediate = 'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[' + @trgTable + ']'') AND type in (N''U''))' set @mySql = @myIntermediate + ' DROP TABLE ' + @trgTable exec(@mySql) --PRINT @mySql --Load Table set @mySql = 'SELECT ' + @xlsFields + ' INTO ' + @trgTable + ' FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;DATABASE=' + @trgPath + '\' + @xlsFile + ';IMEX=1'', ''Select * from ' + @xlsSheet + ''')' + 'WHERE NOT ' + @Firstfield + ' IS NULL' --PRINT @mySql exec(@mySql)
USE [WOOX_CQM] GO /****** Object: StoredProcedure [dbo].[sp_ReadSource] Script Date: 11/21/2014 2:54:33 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[sp_ReadSource] @trgPath varchar(255), @trgTable varchar(255), @trgFields varchar(8000) AS declare @mySql varchar(8000) declare @myIntermediate varchar(8000) declare @Firstfield varchar(100) declare @FirstfieldValue varchar(100) --Lookup lastfield set @Firstfield = dbo.fnGetFirstField(@trgFields) set @FirstfieldValue = RTRIM(LTRIM(SUBSTRING(@Firstfield,2,CHARINDEX(']',@Firstfield,0)-2))) --PRINT @FirstfieldValue --Drop table if exists set @myIntermediate = 'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[' + @trgTable + ']'') AND type in (N''U''))' set @mySql = @myIntermediate + ' DROP TABLE ' + @trgTable exec(@mySql) --PRINT @mySql --Load Table set @mySql = 'SELECT ' + @trgFields + ' INTO ' + @trgTable + ' ' + 'FROM OpenDataSource (''Microsoft.ACE.OLEDB.12.0'', ''Data Source="' + @trgPath + '";Extended properties=Text'')...' + @trgTable + '#txt ' + 'WHERE NOT ' + @Firstfield + ' IS NULL AND ' + @Firstfield + ' <> ' + '''' + @FirstfieldValue + '''' --PRINT @mySql exec(@mySql)
三,文件准备,导入源及Schema.ini配置文件
对于excel文件来说,相对txt格式,实在简单很多,关键不过以下这段代码TRUNCATE TABLE WOOX_INFORMATICA..VBPA; INSERT INTO WOOX_INFORMATICA..VBPA select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\InitialData\db_data.xlsx;HDR=YES', 'SELECT * FROM [VBPA$]') --(适用于office2007及以上版本,2003用Jet - "Microsoft.Jet.OLEDB.4.0")

对于txt格式文件,本人所用格式如下。


这里需要标明的是:域值内,第一行表示数据源文件名;第二行至第六行是必要的设置和说明,每个人按需修改;第七行以下必须按照Col从1索引开始递增,右边是显示的列名及格式。具体有兴趣的同学可以自己试着操作下,看下差异。
四,执行及检查
执行代码如下USE WOOX_CQM EXECUTE sp_ActivateDistributedQueries ----------------------------- SAP Tables ----------------------------- DECLARE @LoadPath varchar(2000) SET @LoadPath = 'C:\InitialData\SAPdata' --import Excel-- EXECUTE sp_ReadXLSSource @LoadPath, 'SAP_TCURF', 'SAP_TCURF.xls', '[Sheet1$]', '[KURST],[FCURR],[TCURR],[GDATU], [FFACT],[TFACT],[FromDate],[ToDate]' --import txt-- EXECUTE sp_ReadSource @LoadPath, 'SAP_TVSTZ', '[VSBED],[LADGR],[WERKS],[LGORT],[VSTEL]'
select * FROM OpenDataSource ('Microsoft.ACE.OLEDB.12.0', 'Data Source="C:\InitialData\SAPdata";Extended properties=Text')...SAP_TW06S#txt

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,
