Monday, March 19, 2012

Insert text file content into database

Hello everyone!

I'm having a problem with inserting the content of a text file into a Sql Server 2005 database.

I'm reading the text file into a dataset, and works fine. What I can't do is what I suspect is the simple part: Insert all the data into a table that has exactly the same configuration that the file. I've never worked with dataset's before, and I can't seem to find the answer to this!

This is what I have done so far:

Dim i2As Integer
Dim jAs Integer
Dim FileAs String = Server.MapPath("..\Docs\Facts\FORM_MAN_V3_1.txt")
Dim TableNameAs String ="Facts"Dim delimiterAs String ="9"Dim resultAs DataSet =New DataSet()
Dim sAs StreamReader =New StreamReader(File)
Dim columnsAs String() = s.ReadLine().Split(Chr(9))
result.Tables.Add(TableName)
Dim strs1As String() = columns
For i2 = 0To CInt(strs1.Length) - 1
Dim colAs String = strs1(i2)
Dim addedAs Boolean =False
Dim [next]As String =""Dim iAs Integer = 0
While Not added
Dim columnnameAs String =String.Concat(col, [next])
columnname = columnname.Replace(Chr(9),"")
If Not result.Tables(TableName).Columns.Contains(columnname)Then result.Tables(TableName).Columns.Add(columnname) added =True
Else i += 1 [next] =String.Concat("_", i.ToString())
End If
End While
Next i2
Dim strs2As String() = s.ReadToEnd().Split(Chr(13) & Chr(10).ToString())
For j = 0To CInt(strs2.Length) - 1
Dim itemsAs String() = strs2(j).Split(Chr(9))

result.Tables(TableName).Rows.Add(items)
Next j

So now I have my dataset populated with all the information, but how can I insert it into the database?

If anyone can help I would appreciate very, very much!

Thank you

Paula

Hi There.

I think you should learn a bit about ado.net's table adapters. They can be created automatically with the dataset designer, and they can be used to select/insert/update/delete data between your dataset and its corresponding database.

Take a look at the howto's at the bottom of this article:

http://msdn2.microsoft.com/en-us/library/tkctt675(VS.80).aspx

No comments:

Post a Comment