Solved

Load string into datatable

Posted on 2008-06-17
12
1,057 Views
Last Modified: 2010-05-18
Hi Experts!

I have a tab delimited string loaded from an http site. This is not a file, it is a string obtained trough and HttpWebRequest and contains serveral lines of tab delimited values.

Which would be the best method to load this into an SQL server table?

Thanks!!!
0
Comment
Question by:glopezz
  • 6
  • 6
12 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 21805827
What is the structure of your table?

The string is tab-delimited ... should each element be a different row?  Or each a different field in a single row?
0
 

Author Comment

by:glopezz
ID: 21805940
The table has the same column structure as the string:

DATE-VALUE1[float]-VALUE2[float]-VALUE3[float]

the string has the same columns per row, but tab delimited.

Thanks!!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 21812104
This should get you close ... I've got some syntax a bit off, but the concepts are here.
dim arVals() as string
dim SQL as string
arVals = Split(TabDelimitedString, vbTab)
 
'set up a command object w/ connection string ... details omitted
 
SQL = "Insert Into MyTable(Field1, Field2, ...FieldN) Values (@Val1, @Val2, ...@ValN)"
 
cmd.CommandText = Sql
 
'next block is psuedocode
foreach Val in arVals
   cmd.Parameters.Append New Param for this value
next
 
cmd.executeNonQuery

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:glopezz
ID: 21812195
Thanks Daniel

Could you be a little more specific on the line:

cmd.Parameters.Append New Param for this value

?

If we have, let's say, 1000 lines in the string, are we sending 1000 sql commands to the server?

Thanks
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 21812608
No, you're adding 1000 parameters to the one statement.

Do you really have 1000 fields in your table?
0
 

Author Comment

by:glopezz
ID: 21812661
This is minute by minute information, at the end of one day we would have 60X60X24 fields.

Could you explain a little more how to add the parameters?

Thanks
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 21812852
Please tell me that really is 60 * 60 * 24 ROWS, not fields!  

Your table is only going to support about 8000 bytes in a given row.

I'll come back in a few minutes w/ code based on the idea of having 1 Row per second, not one field per second.
0
 

Author Comment

by:glopezz
ID: 21813105
Sorry Daniel, I thought you were asking about rows! They are fields :P

0
 

Author Comment

by:glopezz
ID: 21813118
It is 60 * 60 * 24 ROWS, with 4 columns per row.. sorry again! This is how it should work.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 21813322
4 columns -- that sounds good.

What are the 4 columns -- so the code I write will be close to correct for you?
0
 

Author Comment

by:glopezz
ID: 21813553
first one is a datetime, the others are float values (numbers with 3 decimals).
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 21815250
This one should be close
dim arVals() as string
dim SQL as string
dim i as Int32
 
arVals = Split(TabDelimitedString, vbTab)
 
'set up a command object w/ connection string ... details omitted
 
SQL = "Insert Into MyTable(Field1, Field2, Field3, Field4) Values (@Val1, @Val2, @val3, @Val4)"
 
cmd.CommandText = Sql
 
cmd.Parameters.append (new SqlClient.SqlParameter("@Val1", SqlDBType.DateTime
cmd.Parameters.append (new SqlClient.SqlParameter("@Val2", SqlDBType.Float
cmd.Parameters.append (new SqlClient.SqlParameter("@Val3", SqlDBType.Float
cmd.Parameters.append (new SqlClient.SqlParameter("@Val4", SqlDBType.Float
 
for i = 0 to (arVals.LBound-1 ) /4
   cmd.Parameters("@Val1).Value = arVals(i*4)
   cmd.Parameters("@Val2).Value = arVals(i*4) +1
   cmd.Parameters("@Val3).Value = arVals(i*4) +2
   cmd.Parameters("@Val4).Value = arVals(i*4) +3
 
   cmd.ExecuteNonQuery
next i
  

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question