473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

.net method that writes .csv file to SQL Server

Hi,

I want to create a vb.net procedure that opens a .csv file from a specified
directory and then load that file into a SQL server table located on a
remote server. Can someone point me towards a code sample that does this?
Can I set this up to run at a specified time on windows 2000 server, and how
do I do this. I"m familiar with task schedular in XP but don't know what the
equivalent of this is in 2000 server.

Thanks!
Nov 21 '05 #1
17 1467

why use anything other than sql server for this? you can use dts or other
equivalent method to have sql server routinely import the file's data.
"Rasta" <ab*****@netgate.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl...
| Hi,
|
| I want to create a vb.net procedure that opens a .csv file from a
specified
| directory and then load that file into a SQL server table located on a
| remote server. Can someone point me towards a code sample that does this?
| Can I set this up to run at a specified time on windows 2000 server, and
how
| do I do this. I"m familiar with task schedular in XP but don't know what
the
| equivalent of this is in 2000 server.
|
| Thanks!
|
|
Nov 21 '05 #2
I need to do in-depth validation of each record in the csv file before
upload to SQL. This, among other reasons is why I am using .net.
I"m looking for a code sample that loops through the csv file and then
appends it to SQL tables

thanks
"" <a@b.com> wrote in message news:Ji*****************@fe02.lga...
why use anything other than sql server for this? you can use dts or other
equivalent method to have sql server routinely import the file's data.
"Rasta" <ab*****@netgate.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl...
| Hi,
|
| I want to create a vb.net procedure that opens a .csv file from a
specified
| directory and then load that file into a SQL server table located on a
| remote server. Can someone point me towards a code sample that does
this?
| Can I set this up to run at a specified time on windows 2000 server, and
how
| do I do this. I"m familiar with task schedular in XP but don't know what
the
| equivalent of this is in 2000 server.
|
| Thanks!
|
|

Nov 21 '05 #3

here's a c# snip:

Regex r = new Regex(",(?=([^\"]*"[^"]*")*(?![^"]*"))");
string s = "\"a",b,"c, d, e",,f";
int start = 0;
foreach (Match m in r.Matches(s))
{
Console.WriteLine(s.Substring(start, m.Index - start));
start = m.Index + 1;
}
Console.WriteLine(s.Substring(start, s.Length - start));

converts easily and makes for clean, fast, efficient coding.

hth,

me
Nov 21 '05 #4
I would suggest using a csv parser like the one I sell,
http://www.csvreader.com , to loop over the rows one by one, do your
extended validation, and then send the successful rows out to another
csv file. Then, you can send the data into the sql server using dts,
which should be the fastest way. Optionally, you can do single insert
statements on a row by row basis into the database like what you're
asking for, but it will go pretty slow.

Nov 21 '05 #5
Sorry, I have to write this code myself rather than use a third party
package. I'm still looking for a good code sample...

"shriop" <sh****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I would suggest using a csv parser like the one I sell,
http://www.csvreader.com , to loop over the rows one by one, do your
extended validation, and then send the successful rows out to another
csv file. Then, you can send the data into the sql server using dts,
which should be the fastest way. Optionally, you can do single insert
statements on a row by row basis into the database like what you're
asking for, but it will go pretty slow.

Nov 21 '05 #6

i promise the example i posted previously works like a charm.

i wouldn't pay for such a simple operation anyway.

;^)
"Rasta" <ab*****@netgate.net> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
| Sorry, I have to write this code myself rather than use a third party
| package. I'm still looking for a good code sample...
|
| "shriop" <sh****@hotmail.com> wrote in message
| news:11**********************@z14g2000cwz.googlegr oups.com...
| >I would suggest using a csv parser like the one I sell,
| > http://www.csvreader.com , to loop over the rows one by one, do your
| > extended validation, and then send the successful rows out to another
| > csv file. Then, you can send the data into the sql server using dts,
| > which should be the fastest way. Optionally, you can do single insert
| > statements on a row by row basis into the database like what you're
| > asking for, but it will go pretty slow.
| >
|
|
Nov 21 '05 #7
Wy not validate into a table in SQL and then use DTS. I do this often and
it works great. You can set up a stored procedure to do your processing and
at the end call the DTS package from within your stored procedure to create
the CSV file.

Jim
"Rasta" <ab*****@netgate.net> wrote in message
news:en**************@TK2MSFTNGP14.phx.gbl...
I need to do in-depth validation of each record in the csv file before
upload to SQL. This, among other reasons is why I am using .net.
I"m looking for a code sample that loops through the csv file and then
appends it to SQL tables

thanks
"" <a@b.com> wrote in message news:Ji*****************@fe02.lga...
why use anything other than sql server for this? you can use dts or other
equivalent method to have sql server routinely import the file's data.
"Rasta" <ab*****@netgate.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl...
| Hi,
|
| I want to create a vb.net procedure that opens a .csv file from a
specified
| directory and then load that file into a SQL server table located on a
| remote server. Can someone point me towards a code sample that does
this?
| Can I set this up to run at a specified time on windows 2000 server,
and
how
| do I do this. I"m familiar with task schedular in XP but don't know
what
the
| equivalent of this is in 2000 server.
|
| Thanks!
|
|



Nov 21 '05 #8
Am I missing something in your proposed solution? I had to assume a
bunch of escapes just to get it to compile.

Regex r = new
Regex(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*(?!*[^\\\"]*\\\"))");
string s = "\"a\",b,\"c, d, e\",,f";
int start = 0;
foreach (Match m in r.Matches(s))
{
Console.WriteLine(s.Substring(start, m.Index - start));
start = m.Index + 1;
}
Console.WriteLine(s.Substring(start, s.Length - start));

results in this:
"a"
b
"c
d
e"

f

To me, this it totally invalid and doesn't get anywhere. So what'd I
miss?

 wrote:
i promise the example i posted previously works like a charm.

i wouldn't pay for such a simple operation anyway.

;^)
"Rasta" <ab*****@netgate.net> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
| Sorry, I have to write this code myself rather than use a third party
| package. I'm still looking for a good code sample...
|
| "shriop" <sh****@hotmail.com> wrote in message
| news:11**********************@z14g2000cwz.googlegr oups.com...
| >I would suggest using a csv parser like the one I sell,
| > http://www.csvreader.com , to loop over the rows one by one, do your
| > extended validation, and then send the successful rows out to another
| > csv file. Then, you can send the data into the sql server using dts,
| > which should be the fastest way. Optionally, you can do single insert
| > statements on a row by row basis into the database like what you're
| > asking for, but it will go pretty slow.
| >
|
|


Nov 21 '05 #9
I'm sorry, but I don't know C# enough for this solution to help me out at
all (and I"m in a real time crunch to get this done in vb.net). I would
really appreciate it if someone could point me to a code sample for vb.net
or provide a code snippet.

Thanks

"shriop" <sh****@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Am I missing something in your proposed solution? I had to assume a
bunch of escapes just to get it to compile.

Regex r = new
Regex(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*(?!*[^\\\"]*\\\"))");
string s = "\"a\",b,\"c, d, e\",,f";
int start = 0;
foreach (Match m in r.Matches(s))
{
Console.WriteLine(s.Substring(start, m.Index - start));
start = m.Index + 1;
}
Console.WriteLine(s.Substring(start, s.Length - start));

results in this:
"a"
b
"c
d
e"

f

To me, this it totally invalid and doesn't get anywhere. So what'd I
miss?

 wrote:
i promise the example i posted previously works like a charm.

i wouldn't pay for such a simple operation anyway.

;^)
"Rasta" <ab*****@netgate.net> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
| Sorry, I have to write this code myself rather than use a third party
| package. I'm still looking for a good code sample...
|
| "shriop" <sh****@hotmail.com> wrote in message
| news:11**********************@z14g2000cwz.googlegr oups.com...
| >I would suggest using a csv parser like the one I sell,
| > http://www.csvreader.com , to loop over the rows one by one, do your
| > extended validation, and then send the successful rows out to another
| > csv file. Then, you can send the data into the sql server using dts,
| > which should be the fastest way. Optionally, you can do single insert
| > statements on a row by row basis into the database like what you're
| > asking for, but it will go pretty slow.
| >
|
|

Nov 21 '05 #10
On Thu, 4 Aug 2005 13:18:53 -0700, "Rasta" <ab*****@netgate.net> wrote:

¤ Hi,
¤
¤ I want to create a vb.net procedure that opens a .csv file from a specified
¤ directory and then load that file into a SQL server table located on a
¤ remote server. Can someone point me towards a code sample that does this?
¤ Can I set this up to run at a specified time on windows 2000 server, and how
¤ do I do this. I"m familiar with task schedular in XP but don't know what the
¤ equivalent of this is in 2000 server.

You can use SQL w/BULK INSERT:

BULK INSERT Northwind.dbo.[Order Details] FROM 'e:\My Documents\TextFiles\OrderDetails.txt'
WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )

http://msdn.microsoft.com/library/de...ba-bz_4fec.asp

AFAIK, Windows 2000 Server does have a Schedule Tasks Control Panel applet.
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 21 '05 #11

"shriop" <sh****@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Am I missing something in your proposed solution? I had to assume a
bunch of escapes just to get it to compile.

you indeed ARE missing something...like a clue and/or just a LITTLE knowlege
of c# !!!

but for you c# (or otherwise) challenged:

Private Function splitFields(ByVal csv As String) As String()
Dim exp As New Regex(",(?=(?:[^""]*\""[^""]*"")*(?![^""]*""))")
Return exp.split(csv)
End Function

Private Function splitLine(ByVal csv As String) As String()
Dim exp As New Regex("\r\n")
Return exp.Split(csv)
End Function

Public Sub Main()
Dim data As String() = splitLine( _
"""a"",""b"",""c"",""d"",""e"","""",""f"""
& vbCrLf & _
"""g"",""h"",""i"",""j"",""k"","""",""l"""
_
)
If data Is Nothing Then Return
Dim record As String
For Each record In data
Dim fields As String() = splitFields(record)
If Not fields Is Nothing Then
Dim field As String
For Each field In fields
Console.WriteLine(field)
Next
End If
Next
End Sub

Nov 21 '05 #12

oh, and need i keep you from bitching about compile errors by reminding you
to import system.text.regularexpressions?

you truly need to tone down the attitude toward those trying to help others!
"" <a@b.com> wrote in message news:zc****************@fe07.lga...
| "shriop" <sh****@hotmail.com> wrote in message
| news:11**********************@g49g2000cwa.googlegr oups.com...
| Am I missing something in your proposed solution? I had to assume a
| bunch of escapes just to get it to compile.
|
| you indeed ARE missing something...like a clue and/or just a LITTLE
knowlege
| of c# !!!
|
| but for you c# (or otherwise) challenged:
|
| Private Function splitFields(ByVal csv As String) As String()
| Dim exp As New Regex(",(?=(?:[^""]*\""[^""]*"")*(?![^""]*""))")
| Return exp.split(csv)
| End Function
|
| Private Function splitLine(ByVal csv As String) As String()
| Dim exp As New Regex("\r\n")
| Return exp.Split(csv)
| End Function
|
| Public Sub Main()
| Dim data As String() = splitLine( _
|
"""a"",""b"",""c"",""d"",""e"","""",""f"""
| & vbCrLf & _
|
"""g"",""h"",""i"",""j"",""k"","""",""l"""
| _
| )
| If data Is Nothing Then Return
| Dim record As String
| For Each record In data
| Dim fields As String() = splitFields(record)
| If Not fields Is Nothing Then
| Dim field As String
| For Each field In fields
| Console.WriteLine(field)
| Next
| End If
| Next
| End Sub
|
|
|
Nov 21 '05 #13
Ah, the small minded resort to insults when they have nothing intelligent to
say...

Below is an example of what I'm looking for just for your enlightenment.
Maybe this will help you out next time you try to offer one of your pathetic
'solutions'.

Dim lCount As Integer = 0
Ta = fSys.OpenTextFile(Application.StartupPath & "\filename.csv")

Do While Ta.AtEndOfStream = False
Ta.ReadLine()
lCount += 1
Loop

pgBar.Maximum = lCount
pgBar.Value = 0

Ta = fSys.OpenTextFile(Application.StartupPath & "\filename.csv")

Do While Ta.AtEndOfStream = False
strRec = Ta.ReadLine
arrA = Split(strRec, ",")
FillData(arrA)
If pgBar.Value < pgBar.Maximum Then
pgBar.Value += 1
End If

DoEvents()
Loop
Catch ex As Exception
con.Close()
End Try
End Sub

Private Sub FillData(ByVal ArrA As Object)

Dim rsDR1 As SqlClient.SqlDataReader
Scrip = Trim(ArrA(1))
Series = Trim(ArrA(2))
Rate = ArrA(7)
Try
cmd = New SqlClient.SqlCommand("pts_updSRates 'I','" & Scrip &
"','" & CStr(Rate) & "','" _
& Now & "'," & CStr(Rate) & ",'AN'")
cmd.Connection = con
rsDR1 = cmd.ExecuteReader
rsDR1.Read()
If rsDR1.GetString(0) = "Success" Then
End If
rsDR1.Close()
Catch ex As Exception
MsgBox(ex.Message)
rsDR1.Close()
End Try

End Sub

ps: you may want to debug your 'code' before posting next time, Einstein.

"" <a@b.com> wrote in message news:zc****************@fe07.lga...
"shriop" <sh****@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Am I missing something in your proposed solution? I had to assume a
bunch of escapes just to get it to compile.

you indeed ARE missing something...like a clue and/or just a LITTLE
knowlege
of c# !!!

but for you c# (or otherwise) challenged:

Private Function splitFields(ByVal csv As String) As String()
Dim exp As New Regex(",(?=(?:[^""]*\""[^""]*"")*(?![^""]*""))")
Return exp.split(csv)
End Function

Private Function splitLine(ByVal csv As String) As String()
Dim exp As New Regex("\r\n")
Return exp.Split(csv)
End Function

Public Sub Main()
Dim data As String() = splitLine( _

"""a"",""b"",""c"",""d"",""e"","""",""f"""
& vbCrLf & _

"""g"",""h"",""i"",""j"",""k"","""",""l"""
_
)
If data Is Nothing Then Return
Dim record As String
For Each record In data
Dim fields As String() = splitFields(record)
If Not fields Is Nothing Then
Dim field As String
For Each field In fields
Console.WriteLine(field)
Next
End If
Next
End Sub

Nov 21 '05 #14

| Private Function splitFields(ByVal csv As String) As String()
| Dim exp As New Regex(",(?=(?:[^""]*\""[^""]*"")*(?![^""]*""))")
| Return exp.split(csv)
| End Function

this function will still work, however i just noticed i left in 1
backslash...the corrected pattern is:

",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
Nov 21 '05 #15
So this is you trying to 'help'- Do all of us a favor and stop.

"" <a@b.com> wrote in message news:zf***************@fe07.lga...
oh, and need i keep you from bitching about compile errors by reminding
you
to import system.text.regularexpressions?

you truly need to tone down the attitude toward those trying to help
others!
"" <a@b.com> wrote in message news:zc****************@fe07.lga...
| "shriop" <sh****@hotmail.com> wrote in message
| news:11**********************@g49g2000cwa.googlegr oups.com...
| Am I missing something in your proposed solution? I had to assume a
| bunch of escapes just to get it to compile.
|
| you indeed ARE missing something...like a clue and/or just a LITTLE
knowlege
| of c# !!!
|
| but for you c# (or otherwise) challenged:
|
| Private Function splitFields(ByVal csv As String) As String()
| Dim exp As New Regex(",(?=(?:[^""]*\""[^""]*"")*(?![^""]*""))")
| Return exp.split(csv)
| End Function
|
| Private Function splitLine(ByVal csv As String) As String()
| Dim exp As New Regex("\r\n")
| Return exp.Split(csv)
| End Function
|
| Public Sub Main()
| Dim data As String() = splitLine( _
|
"""a"",""b"",""c"",""d"",""e"","""",""f"""
| & vbCrLf & _
|
"""g"",""h"",""i"",""j"",""k"","""",""l"""
| _
| )
| If data Is Nothing Then Return
| Dim record As String
| For Each record In data
| Dim fields As String() = splitFields(record)
| If Not fields Is Nothing Then
| Dim field As String
| For Each field In fields
| Console.WriteLine(field)
| Next
| End If
| Next
| End Sub
|
|
|

Nov 21 '05 #16
Thanks, I'll give that a try.
"Paul Clement" <Us***********************@swspectrum.com> wrote in message
news:iv********************************@4ax.com...
On Thu, 4 Aug 2005 13:18:53 -0700, "Rasta" <ab*****@netgate.net> wrote:

¤ Hi,
¤
¤ I want to create a vb.net procedure that opens a .csv file from a
specified
¤ directory and then load that file into a SQL server table located on a
¤ remote server. Can someone point me towards a code sample that does
this?
¤ Can I set this up to run at a specified time on windows 2000 server, and
how
¤ do I do this. I"m familiar with task schedular in XP but don't know what
the
¤ equivalent of this is in 2000 server.

You can use SQL w/BULK INSERT:

BULK INSERT Northwind.dbo.[Order Details] FROM 'e:\My
Documents\TextFiles\OrderDetails.txt'
WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )

http://msdn.microsoft.com/library/de...ba-bz_4fec.asp

AFAIK, Windows 2000 Server does have a Schedule Tasks Control Panel
applet.
Paul
~~~~
Microsoft MVP (Visual Basic)

Nov 21 '05 #17


"Rasta" <ab*****@netgate.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
| Ah, the small minded resort to insults when they have nothing intelligent
to
| say...

the insult was not to you, rasta. i'm trying to help you.

| Below is an example of what I'm looking for just for your enlightenment.
| Maybe this will help you out next time you try to offer one of your
pathetic
| 'solutions'.

you asked for the c# code to be converted to vb.net...so i did. did you not
run the code and test it? did it not work? i'd usually stay away from biting
the hand that feeds you...i'm going to assume your response is a reaction to
the *perceived* insult you think i threw your way.

<snip lots of code>

| ps: you may want to debug your 'code' before posting next time, Einstein.

true enough...however, i assumed you were familiar enough with c# to
correctly convert the example to vb.net. it compiles and runs fine in either
example i gave.

again, i'll ignore the "einstein" insult as i feel it is a reactionary
defensive comment. you'll be able to clear up this assumption in your next
post. if i'm correct, i'll be happy to help you with integrating the example
with your specific validation routine or even setting up a dts package...if
i'm NOT correct and you truly intend to be insulting, i'll quietly go away.

hth,

me
Nov 21 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Manuel Lopez | last post by:
From what I've read in the docs, ado.net currently supports opening sql server ce tables in table-direct mode and performing Seek operations on them (using SqlCeDataReader), but not on the...
5
by: Vishal | last post by:
Hello, I already asked this question in the ASP.NET forums, but no help came. So I am hoping that somebody can help me out. This is really very URGENT me. For my e-commerce application, I...
2
by: Ian Toltz | last post by:
I've got this bizarre problem with a program I'm making... I've got a kludgy mickey mouse solution for communicating with a server... The program writes to a file, and a script on the server...
2
sanjib65
by: sanjib65 | last post by:
I try to create a custom control that will say, Hello + ComputerUserName. I have added in my App_Code folder a WelcomeLabel.cs file: // WelcomeLabel.cs using System; using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.