473,385 Members | 1,838 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,385 software developers and data experts.

text file to datatable to SQL2005 table not working

I am having a problem moving the data from a datatable to the SQL2005
table (using VB2005). See code below. The SQL2005 table is empty, the
datatable is being filled from a text file, not from the SQL2005 table.
I have tried various ways, but the SQL table is not updating. Help
would be appreciated.
Code is below
Thanks

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Net
Imports System.IO
Imports SYGNeT3.clsFTP
Imports System.Text
Imports System.Net.Sockets

Public Class Utility
'************************
' other stuff here
'*************************

Public Shared Function ReadOGText() As Boolean
Dim MyTime As System.DateTime = "00:00:00"
Console.WriteLine("Start " & Now())
Dim ctr As Int32 = 0
Dim strMsg As String = ""
Dim RetVal As Integer = 0
Dim ConStr As String = GetConnectionString()
Dim myConnection As New SqlConnection(ConStr)
If Not (myConnection.State = ConnectionState.Open) Then
myConnection.Open()
Dim sSQL As String = "DELETE FROM tblItems" ' Clean out table
before load from text file
Dim myCommand As New SqlCommand(sSQL, myConnection)
RetVal = myCommand.ExecuteNonQuery()
Dim ds As New DataSet()
Dim dt As New DataTable("temp")
Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM
tblItems", ConStr)
ds.Clear()
da.FillSchema(ds, SchemaType.Mapped, "temp")
Dim path As String = AppPath(True) & "Incoming\OG.txt"
Dim sR As IO.StreamReader =
System.IO.File.OpenText(path.ToString)
Dim fileline As String = ""
Try
Do While sR.Peek <> -1
fileline = sR.ReadLine
Dim dr As DataRow = ds.Tables("temp").NewRow
dr("CompanyNumber") =
Convert.ToInt32(fileline.Substring(0, 4)) ' 0-4
dr("PriceGroup") =
Convert.ToInt16(fileline.Substring(5, 7)) '5-11
dr("ItemCategoryCode") =
Convert.ToInt32(fileline.Substring(12, 5)) '12-16
dr("ItemNumber") =
Convert.ToInt32(fileline.Substring(17, 9)) '17-25
dr("ItemDescription") = fileline.Substring(26,
30).TrimEnd '26-55

'************************************************* ******
' the other 400 chars work as well, removed for example
'************************************************* *******
dr("New") = True
dr("Date") = Now()
ds.Tables("temp").Rows.Add(dr)
ctr += 1
Loop
Console.WriteLine("Rows = " & CStr(ctr))
'************************************************* *********************

' This where I am having a problem, getting the data from the temp
table to the
' table in the SQL2005 mdf. I am using VB2005
'************************************************* *********************

Try
sSQL = "INSERT INTO TBLITEMS "
sSQL &= "SELECT temp.* "
sSQL &= "FROM temp;"
Dim myCommand2 As New SqlCommand(sSQL, myConnection)
RetVal = myCommand2.ExecuteNonQuery()
'ds.HasChanges() 'false
'da.Fill(ds, "temp")
'da.Update(ds, "tblItems")
Catch e As Exception
'TODO: PutInfo() add error message here
Console.WriteLine(e.Message)
End Try
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
If Not (myConnection.State = ConnectionState.Closed) Then
myConnection.Close()
sR.Close()
sR = Nothing
GC.Collect()
ReadOGText = True
End Try
Console.WriteLine("End " & Now())
End Function
End Class

Jun 9 '06 #1
1 2721

"r1100r98" <pa*****@gmail.com> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...
I am having a problem moving the data from a datatable to the SQL2005
table (using VB2005). See code below. The SQL2005 table is empty, the
datatable is being filled from a text file, not from the SQL2005 table.
I have tried various ways, but the SQL table is not updating. Help
would be appreciated.
Code is below
Thanks

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Net
Imports System.IO
Imports SYGNeT3.clsFTP
Imports System.Text
Imports System.Net.Sockets

Public Class Utility
'************************
' other stuff here
'*************************

Public Shared Function ReadOGText() As Boolean
Dim MyTime As System.DateTime = "00:00:00"
Console.WriteLine("Start " & Now())
Dim ctr As Int32 = 0
Dim strMsg As String = ""
Dim RetVal As Integer = 0
Dim ConStr As String = GetConnectionString()
Dim myConnection As New SqlConnection(ConStr)
If Not (myConnection.State = ConnectionState.Open) Then
myConnection.Open()
Dim sSQL As String = "DELETE FROM tblItems" ' Clean out table
before load from text file
Dim myCommand As New SqlCommand(sSQL, myConnection)
RetVal = myCommand.ExecuteNonQuery()
Dim ds As New DataSet()
Dim dt As New DataTable("temp")
Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM
tblItems", ConStr)
ds.Clear()
da.FillSchema(ds, SchemaType.Mapped, "temp")
Dim path As String = AppPath(True) & "Incoming\OG.txt"
Dim sR As IO.StreamReader =
System.IO.File.OpenText(path.ToString)
Dim fileline As String = ""
Try
Do While sR.Peek <> -1
fileline = sR.ReadLine
Dim dr As DataRow = ds.Tables("temp").NewRow
dr("CompanyNumber") =
Convert.ToInt32(fileline.Substring(0, 4)) ' 0-4
dr("PriceGroup") =
Convert.ToInt16(fileline.Substring(5, 7)) '5-11
dr("ItemCategoryCode") =
Convert.ToInt32(fileline.Substring(12, 5)) '12-16
dr("ItemNumber") =
Convert.ToInt32(fileline.Substring(17, 9)) '17-25
dr("ItemDescription") = fileline.Substring(26,
30).TrimEnd '26-55

'************************************************* ******
' the other 400 chars work as well, removed for example
'************************************************* *******
dr("New") = True
dr("Date") = Now()
ds.Tables("temp").Rows.Add(dr)
ctr += 1
Loop
Console.WriteLine("Rows = " & CStr(ctr))
'************************************************* *********************

' This where I am having a problem, getting the data from the temp
table to the
' table in the SQL2005 mdf. I am using VB2005
'************************************************* *********************


Look as the SqlBulkCopy object.

David

Jun 9 '06 #2

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

Similar topics

7
by: Chris | last post by:
Hi I can use a text file as a datasource but am unable to get the datatable to see the text file as having multiple columns. Everything gets put into the first column in the datatable. Sample of...
8
by: John Wildes | last post by:
Hello all I'm going to try and be brief with my question, please tell me if I have the wrong group. We are querying transaction data from a DB3 database application. The dates are stored as...
11
by: scorpion53061 | last post by:
Well I had a way to write an array to an excel spreadsheet but on a huge time critical run it failed iwth the dreaded HRESULT: 0x800A03EC error. It worked fine when i sampled the data to go in but...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
13
by: JJ | last post by:
I have a need to input a large tab delimited text file, which I will parse to check it has the expected columns, before allowing the user to submit it to the database. The user may paste the file...
2
by: Roger | last post by:
I've got two tables in sql2005 which have an 'ntext' field when I linked the first table in access97 last week using an odbc data source the access-field type was 'memo' when I link the 2nd...
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.