How to import Excel Data to SQL database in Delphi

Source: Internet
Author: User
Tags mdb database

 

VaR
S1: string;
Begin
Screen. cursor: = crsqlwait;
S1: = 'insert into table_bus '+
'(Bs_no, bs_type, bs_whos, bs_mode, bs_seat, bs_soalseat, bs_drseat, bs_oiltype, bs_buycard, bs_unit, bs_bdate, bs_edate)' +
'Select car number, DBO. uf_getfullnamecode (vehicle model name), DBO. uf_getfullnamecode (unit), DBO. uf_getfullnamecode (factory model), approved seats, ticket seats, front seats in the cab, DBO. uf_getfullnamecode (fuel name), purchase certificate number, external unit, start date, DBO. uf_convertnulldate (End Time) '+
'From OpenDataSource (''Microsoft. jet. oledb.4.0 '', ''data source =" '+ trim (labelededit1.text) +' "; extended properties = Excel 8.0; persist Security info = false '')... [sheet1 $] '+
'Where car no. Not in (select bs_no from table_bus )';
Try
Datamodule1.adoconnection1. Execute (S1 );
Finally
Screen. cursor: = crarrow;
End;
End;

 

Excel can be processed as a data source. STR: = 'provider = Microsoft. Jet. oledb.4.0; Data Source = '+ file path +
'; Extended properties = Excel 8.0; persist Security info = false ';
Then each column is equivalent to a field

 

Database Administrators familiar with SQL Server 2000 know that DTS can import and export data. In fact, we can also use Transact-SQL statements to import and export data. In a Transact-SQL statement, we mainly use the OpenDataSource function and OpenRowset function. For more information about functions, see SQL online help. You can easily convert SQL Server, access, and Excel data by using the following methods:

First. Import and export data from SQL Server and access
General data import and export:
Use the DTS wizard to migrate your access data to SQL Server. You can use these steps:
○ 1. On the Tools menu in SQL Server Enterprise Manager, select data transformation
○ 2 Services (data conversion service), and then select czdimport data (import data ).
○ 3 select Microsoft Access as the source in the choose a data source dialog box, and then type your. mdb database (. MDB File Extension) or browse to find the file.
○ 4 in the choose a destination (select target) dialog box, select Microsoft ole db prov ider for SQL Server, select database server, and click the required authentication method.
○ 5. In the specify table copy (specifying table copy) or query dialog box, click Copy tables ).
○ 6 in the select Source tables dialog box, click Select All ). Next, complete.

Use a Transact-SQL statement to import and export data:
1. query access data in SQL Server:

Select *
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/DB. mdb"; user id = admin; Password = ')... table name

2. Import access to SQL Server
Run in SQL Server:
Select *
Into newtable
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/DB. mdb"; user id = admin; Password = ')... table name

3. insert data in the SQL Server table to the Access Table
Run in SQL Server:
Insert into OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/DB. mdb"; user id = admin; Password = ')... table name
(Column name 1, column name 2)
Select column name 1, column name 2 from SQL table

Instance:
Insert into OpenRowSet ('Microsoft. Jet. oledb.4.0 ',
'C:/DB. mdb '; 'admin'; '', test)
Select ID, name from test

Insert into OpenRowSet ('Microsoft. Jet. oledb.4.0 ', 'c:/trade. mdb'; 'admin'; '', table name)
Select *
From sqltablename

Second. Import and export data from SQL Server and Excel

1. query Excel Data in SQL Server:

Select *
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/book1.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... [sheet1 $]

The following is an example of a query. It queries an Excel spreadsheet through the ole db provider for jet.
Select *
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/finance/account.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... xactions

2. Import Excel Data to SQL Server:
Select * into newtable
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/book1.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... [sheet1 $]

Instance:
Select * into newtable
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/finance/account.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... xactions

3. Export the data queried in SQL Server into an Excel file
T-SQL code:
Exec master.. xp_mongoshell 'bcp database name. DBO. Table name out C:/temp.xls-C-Q-s "servername"-U "sa"-P ""'
Parameter: s indicates the SQL server name, u indicates the user, and P indicates the password.
Note: You can also export text files and other formats.

Instance: exec master .. xp_mongoshell 'bcp saletesttmp. DBO. cusaccount out C:/temp1.xls-C-Q-s "pmserver"-U "sa"-P "sa "'

Exec master .. xp_mongoshell 'bcp "select au_fname, au_lname from pubs .. authors order by au_lname" queryout C:/authors.xls-C-sservername-USA-ppassword'

Use ADO to export the Excel file code in VB6:
Dim cn as new ADODB. Connection
CN. Open "driver = {SQL Server}; server = websvr; database = webmis; uid = sa; WD = 123 ;"
Cn.exe cute "master .. xp_mongoshell 'bcp" select col1, col2 from database name. DBO. Table Name "queryout E:/dt.xls-C-sservername-USA-ppassword '"

4. insert data to excel in SQL Server:

Insert into OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C:/temp.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... table1 (A1, A2, A3) values (1, 2, 3)

T-SQL code:
Insert
OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Extended properties = Excel 8.0; Data Source = C:/training/inventur.xls ')... [filiale1 $]
(Bestand, Produkt) values (20, 'test ')

Summary: with the above statements, we can easily convert data in SQL Server, access, and Excel spreadsheet software, which provides us with great convenience!

 

Method 2:
Procedure tform1.button3click (Sender: tobject );
VaR
OE: variant;
N1, STR: string;
J: integer;
Begin
If opendialog1.execute then
Opendialog1.defaultext: = 'xls ';
STR: = opendialog1.filename;
OE: = createoleobject ('excel. application ');
Oe. Visible: = true;
Oe. workbooks. Open (STR );
J: = 2; // assume that the title is in the first line.
Try
With adoquery2 do
Begin
N1: = 'select * from # tbl_cardgrant1 ';
Close;
SQL. Clear;
SQL. Add (N1 );
While Oe. worksheets ['query result 1']. cells [J, 1]. Text <> ''do
Begin
For I: = 1 to adoquery1.fieldcount do
Parameters. items [I-1]. Value: = Oe. worksheets ['query result 1']. cells [J, 1]. text;
Memo2.text: = N1;
Execsql;
J: = J + 1;
End;
Oe. Visible: = false;
Showmessage ('import finished ');
End;
Except
Oe. Visible: = false;
Showmessage ('database error or repeated number; check ');
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.