1

I want to make a program in C# that imports two types of files to SQL Server: tab delimited and fixed columns. Actually, I need to download a file every day and import that file into my database. I could make a console app with batch script. I saw some examples like this, but I don´t know if it is the best object-oriented way to do it.

I could use StreamReader, Regex and so on, but I don't want to re-invent the wheel.

PS: In VBA I used "QueryTables.Add".

Aacini
  • 65,180
  • 12
  • 72
  • 108
Makah
  • 4,435
  • 3
  • 47
  • 68
  • Could you show the format for what the file is supposed to look like related to the SQL server? – msarchet Nov 24 '11 at 21:31
  • Please note that Batch tag is "used for Windows batch file questions". Don't use it just because your question is related to "batch" word in any way... – Aacini Nov 25 '11 at 02:39

4 Answers4

2

You can import in fully managed code via SqlBulkCopy; all you need to do is pass SqlBulkCopy an IDataReader that handles TSV. Fortunately the FastCsvReader on codeproject can do exactly that.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I have some trash rows in my file. I need something like row patterns (Regex). I could create some file converter then use SQLBulk – Makah Nov 25 '11 at 15:10
1

If you don't want to reinvent the wheel, then you should look at the native tools that SQL Server provides for this, namely bcp. Here is a list of FAQs about bcp.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0
bulk insert [dbo].[CoursesTemp]

from 'C:\Users\Public\Downloads\Courses.csv'

with (fieldterminator = ',', rowterminator = '\n')
go
insert [dbo].[Courses]
  (code, description, instructor, date, venue, duration)
select 
   code, description, instructor, cast(date as date), venue,
   duration
from [dbo].[CoursesTemp]
Anthon
  • 69,918
  • 32
  • 186
  • 246
0

Sounds like a perfect job for SQL Server Integration Services (SSIS). You can easily define a data import task in SSIS and then schedule it to run by using a SQL job.

tom redfern
  • 30,562
  • 14
  • 91
  • 126