PNRT
asked on
How to get one variable into SQL Express
Hi all, Using vb.net/2005/.net 2 - I have a simple program that produces large numbers of one variable of 110 characters. There is no user involvement. I need to get the variable into a field in SQL Express 2005 where it is then broken up into its constituent parts. The variables are produced quite rapidly so the solution needs to be fast and stable. Would appreciate some instruction and code samples of the best method. Thanks in advance.
I am unable to understand your description so would appreciate if you could provide sonme more information.
ASKER
Thanks for the reply.
I have a VB.Net program that produces a string that is then output as a simple line of text 110 characters long. Currently this line of text is sent to a text file at a rate of about 2000 lines per minute. I am running SQL Express on the same server. Instead of outputing this data to a text file, I would like to send it to a database in SQL Express. There is no user input in the program, the data is produced automatically. I am just looking for the quickest and most reliable way of connecting to the database and getting the line of text into the required column, but at the speed and volumes I have mentioned. Thanks.
I have a VB.Net program that produces a string that is then output as a simple line of text 110 characters long. Currently this line of text is sent to a text file at a rate of about 2000 lines per minute. I am running SQL Express on the same server. Instead of outputing this data to a text file, I would like to send it to a database in SQL Express. There is no user input in the program, the data is produced automatically. I am just looking for the quickest and most reliable way of connecting to the database and getting the line of text into the required column, but at the speed and volumes I have mentioned. Thanks.
ASKER
I thought I would also include the code that sends to the text file. Very simple as you can see. I would like to find the best way of getting Str2Write into SQL - Thanks
Private Sub Write_To_File(ByVal Str2Write As String)
If File.Exists("C:\DATA\Data. txt") = False Then
fs1 = File.CreateText("c:\DATA\D ata.txt")
fs1.Close()
fs1 = Nothing
End If
fs1 = File.AppendText("C:\DATA\D ata.txt")
fs1.Write(Str2Write)
fs1.Close()
fs1 = Nothing
End Sub
Private Sub Write_To_File(ByVal Str2Write As String)
If File.Exists("C:\DATA\Data.
fs1 = File.CreateText("c:\DATA\D
fs1.Close()
fs1 = Nothing
End If
fs1 = File.AppendText("C:\DATA\D
fs1.Write(Str2Write)
fs1.Close()
fs1 = Nothing
End Sub
Try with this code. Note that if this code snippet is to be called 2000 times a minute, it would be worth it to have the connection created and opened outside of this function and just executing the command within this function because creating a connection could be a overhead.
Dim dbcon as new SqlConnection(connectionstring)
dbcon.open
dim dbcmd as new SqlCommand
dbcmd.connection = dbcon
dbcmd.commandtext = "Insert Into TableName(ColumnName) Values('" & str2write & "')"
dbcmd.executenonquery
'If you want to write this line multiple times then
for i as integer = 0 to 2000
dbcmd.commandtext = "Insert Into TableName(ColumnName) Values('" & str2write & "')"
dbcmd.executenonquery
next
ASKER
It worked first time CC - Many Thanks. As I mentioned, there is a lot of data and I really like the advice about reducing the overhead by creating and opening the connection seperately. Could you please show a way this could be done? Would it be a seperate function or something?
It depends on how and where your data is being generated. Can you show me some code?
ASKER
Hi
This is a legacy system that I'm trying to upgrade piece by piece as the system is still being used and
I cant get into the code of the original program. Three old programs produce string data into text files with sequential filenames. My program combines these into a single text file, with other data in a particular format that is used locally. As you can imagine there is a nightly backlog as the system is very slow, but as I replace the original programs getting away from text files, the process will hopefully be much quicker. I have tested your example into SQL and it works fine, its just to follow your suggestion of creating and opening the connection seperately.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim account_File As String = ""
Dim Finance_File As String = ""
Dim MKT_File As String = ""
Dim Str2Write As String = ""
Dim fs1 As StreamWriter
'SEQU is obtained and saved when program opens and closes
account_File = "C:\DATA\Accounts" & SEQU & ".txt"
Finance_File = "C:\DATA\Finance" & SEQU & ".txt"
MKT_File = "C:\DATA\MKT" & SEQU & ".txt"
Dim Accounts As String = ""
Dim Finance As String = ""
Dim MKT As String = ""
If File.Exists(Finance_File) = False Or File.Exists(account_File) = False Or File.Exists(MKT_File) = False Then
Exit Sub
End If
Accounts = My.Computer.FileSystem.Rea dAllText(a ccount_Fil e)
Finance = My.Computer.FileSystem.Rea dAllText(F inance_Fil e)
MKT = My.Computer.FileSystem.Rea dAllText(M KT_File)
File.Delete(account_File)
File.Delete(Finance_File)
File.Delete(MKT_File)
Str2Write = Now & Accounts & Finance & MKT & Lead_Form 'Lead_Form from formula elsewhere in prgram
If File.Exists("C:\DATA\Data. txt") = False Then
fs1 = File.CreateText("c:\DATA\D ata.txt")
fs1.Close()
fs1 = Nothing
End If
fs1 = File.AppendText("C:\DATA\D ata.txt")
fs1.Write(Str2Write)
fs1.Close()
fs1 = Nothing
SEQU = SEQU + 1
End Sub
This is a legacy system that I'm trying to upgrade piece by piece as the system is still being used and
I cant get into the code of the original program. Three old programs produce string data into text files with sequential filenames. My program combines these into a single text file, with other data in a particular format that is used locally. As you can imagine there is a nightly backlog as the system is very slow, but as I replace the original programs getting away from text files, the process will hopefully be much quicker. I have tested your example into SQL and it works fine, its just to follow your suggestion of creating and opening the connection seperately.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim account_File As String = ""
Dim Finance_File As String = ""
Dim MKT_File As String = ""
Dim Str2Write As String = ""
Dim fs1 As StreamWriter
'SEQU is obtained and saved when program opens and closes
account_File = "C:\DATA\Accounts" & SEQU & ".txt"
Finance_File = "C:\DATA\Finance" & SEQU & ".txt"
MKT_File = "C:\DATA\MKT" & SEQU & ".txt"
Dim Accounts As String = ""
Dim Finance As String = ""
Dim MKT As String = ""
If File.Exists(Finance_File) = False Or File.Exists(account_File) = False Or File.Exists(MKT_File) = False Then
Exit Sub
End If
Accounts = My.Computer.FileSystem.Rea
Finance = My.Computer.FileSystem.Rea
MKT = My.Computer.FileSystem.Rea
File.Delete(account_File)
File.Delete(Finance_File)
File.Delete(MKT_File)
Str2Write = Now & Accounts & Finance & MKT & Lead_Form 'Lead_Form from formula elsewhere in prgram
If File.Exists("C:\DATA\Data.
fs1 = File.CreateText("c:\DATA\D
fs1.Close()
fs1 = Nothing
End If
fs1 = File.AppendText("C:\DATA\D
fs1.Write(Str2Write)
fs1.Close()
fs1 = Nothing
SEQU = SEQU + 1
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.