0

I have files (like plain text) that I should insert into SQL using C#.

They are headerless, and even without extension. Only info I have is starting index of every column.

Any help how can I do this? Ok, I know I will need to separate columns and insert them in datatable when I read file, but how to read those rows?

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
el ninho
  • 4,183
  • 15
  • 56
  • 77
  • `File.ReadAllLines`/`String.Split`? Have you tried anything? Where is sample of line you are trying to parse? – Alexei Levenkov Nov 19 '13 at 19:41
  • @AlexeiLevenkov If you find this question insulting, please pass it. I asked how to open file, I'll know how to parse it. And thanks, that should probably do it. – el ninho Nov 19 '13 at 19:44
  • 1
    "I have...starting index of every column" - Sounds like you are dealing with a fixed width format: http://stackoverflow.com/questions/162727/read-fixed-width-record-from-text-file – AaronLS Nov 19 '13 at 19:47
  • Searching MSDN answers many questions like this one. Also see this page titles "Quick Technology Finder for the .NET Framework", for a link labeled "I/O": http://msdn.microsoft.com/en-us/library/63bf39c2(v=vs.110).aspx Documentation for .NET does exist. – Steve Nov 19 '13 at 21:10
  • @elninho - no, I found your question lacking effort... Note that if you check text of you post you'll not find anything that says that "I asked how to open file". There is "how to read those rows" but it does not in any way shows that you don't know how to read text from file... For future questions try to re-read your question after posting and see if it reflects what you actually wanted to know. – Alexei Levenkov Nov 19 '13 at 23:05

1 Answers1

2

You need to read up on SQL Server's bcp.exe utility (bulk copy) and its SQL statement brother bulk insert and their C# sister, SqlBulkCopy.

As far as creating a custom DataTable goes, read the documentation. Don't forget to invoke AcceptChanges() after creating the columns you want, and after adding the data you want to add.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135