Friday, March 9, 2012

Import data from a text file with DELPHI toward sql server

Hello,
Is there a way to import data from a text file with DELPHI toward a SQL Server table.
I heard about BCP ...?
Does someone know?
THANKS.I'll bet that one of us could figure it out, given a few more details. There are several ways to import text files into SQL Server, but BCP is a good place to start!

-PatP|||Ok no problem,

I have a table with one columns named 'insee' , I want to feed this table by extracting the value of this column from a text file.

I want to automate the process from a DELPHI GUI.

The GUI would seek through the arborescens the file, the user would choose the text file he want to import for ex 'data.txt' and click on 'import'

Then the 'insee' column of 'data.txt' would be include in a one column table wich already exist on sql server.

Thank you.|||There are a number of ways to do what you want. I'll give you a few examples, and you can either pick one of them, or propose a new one.

I think that the easiest way would be if the SQL Server can open the file using a UNC name. If that is the case, the server itself can do the BCP for you using a UNC to reach the file. This is very efficient, very low impact on the network, and easy to code.

My next suggestion would be to have BCP available on each workstation running your Delphi program. You could then shell out to run BCP, sending the file directly from the workstation to the server. This is very secure, very low impact on the network, and easy to code.

My last suggestion would be to write the application to use the Bulk Copy API to load the file. Personally this would probably be my first choice, but not everyone knows how to use the BCP API, and it isn't very straightforward the first few times that you use it. This is very secure, very low impact on the network, but it is not easy to code.

One thing that you might want to consider is the kind of data you are moving, and its source and destination. There are some countries (notably France) where I'm pretty sure it is still a crime to BCP data across the border!

-PatP|||Hello,

Thank you Pat Phelan.

I think I'm going to use bcp, even if I am in France lol

I've made some try, but don't manage with bcp to import only one column of the text file.

Ideed there is no pb if the table on my sql server has the same columns as the text file, but since the moment there are differents it does't work.

Is there a way ( maybe with a particular format file .fmt) of importing only one column of the text file?

Thank you!|||You'll need to replace IN woth FORMAT nad include "-f<format_file_name.ext>" to generate the format file. Then you can edit the format file to import only specific column(-s). Alternatively, you can generate the format file on the fly programmatically when user selects a file to import.

No comments:

Post a Comment