无论用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)存储过程:sp_ReadSource
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")其它需要注意的不过就是保证excel内的数据格式是统一的文本格式,这里只简单给出事例图,不做详细说明。

对于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]'可用以下代码先行测试导入数据是否有差距,同学们记得更改本人所用的hardcored。
select * FROM OpenDataSource ('Microsoft.ACE.OLEDB.12.0', 'Data Source="C:\InitialData\SAPdata";Extended properties=Text')...SAP_TW06S#txtok,大概代码就是这样,如果有什么不懂或者有不同意见的,可用留言讨论!

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
