Import flat file to SQL server using C#
-
08-03-2021 - |
Question
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".
OTHER TIPS
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.
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.
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]