473,378 Members | 1,541 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,378 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 2720

"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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
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...

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.