473,692 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sql Client
Imports System.Data.Ole Db
Imports System.Net
Imports System.IO
Imports SYGNeT3.clsFTP
Imports System.Text
Imports System.Net.Sock ets

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

Public Shared Function ReadOGText() As Boolean
Dim MyTime As System.DateTime = "00:00:00"
Console.WriteLi ne("Start " & Now())
Dim ctr As Int32 = 0
Dim strMsg As String = ""
Dim RetVal As Integer = 0
Dim ConStr As String = GetConnectionSt ring()
Dim myConnection As New SqlConnection(C onStr)
If Not (myConnection.S tate = ConnectionState .Open) Then
myConnection.Op en()
Dim sSQL As String = "DELETE FROM tblItems" ' Clean out table
before load from text file
Dim myCommand As New SqlCommand(sSQL , myConnection)
RetVal = myCommand.Execu teNonQuery()
Dim ds As New DataSet()
Dim dt As New DataTable("temp ")
Dim da As New SqlClient.SqlDa taAdapter("SELE CT * FROM
tblItems", ConStr)
ds.Clear()
da.FillSchema(d s, SchemaType.Mapp ed, "temp")
Dim path As String = AppPath(True) & "Incoming\OG.tx t"
Dim sR As IO.StreamReader =
System.IO.File. OpenText(path.T oString)
Dim fileline As String = ""
Try
Do While sR.Peek <> -1
fileline = sR.ReadLine
Dim dr As DataRow = ds.Tables("temp ").NewRow
dr("CompanyNumb er") =
Convert.ToInt32 (fileline.Subst ring(0, 4)) ' 0-4
dr("PriceGroup" ) =
Convert.ToInt16 (fileline.Subst ring(5, 7)) '5-11
dr("ItemCategor yCode") =
Convert.ToInt32 (fileline.Subst ring(12, 5)) '12-16
dr("ItemNumber" ) =
Convert.ToInt32 (fileline.Subst ring(17, 9)) '17-25
dr("ItemDescrip tion") = fileline.Substr ing(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.WriteLi ne("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.Exec uteNonQuery()
'ds.HasChanges( ) 'false
'da.Fill(ds, "temp")
'da.Update(ds, "tblItems")
Catch e As Exception
'TODO: PutInfo() add error message here
Console.WriteLi ne(e.Message)
End Try
Catch ex As Exception
Console.WriteLi ne(ex.Message)
Finally
If Not (myConnection.S tate = ConnectionState .Closed) Then
myConnection.Cl ose()
sR.Close()
sR = Nothing
GC.Collect()
ReadOGText = True
End Try
Console.WriteLi ne("End " & Now())
End Function
End Class

Jun 9 '06 #1
1 2737

"r1100r98" <pa*****@gmail. com> wrote in message
news:11******** *************@y 43g2000cwc.goog legroups.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.Sql Client
Imports System.Data.Ole Db
Imports System.Net
Imports System.IO
Imports SYGNeT3.clsFTP
Imports System.Text
Imports System.Net.Sock ets

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

Public Shared Function ReadOGText() As Boolean
Dim MyTime As System.DateTime = "00:00:00"
Console.WriteLi ne("Start " & Now())
Dim ctr As Int32 = 0
Dim strMsg As String = ""
Dim RetVal As Integer = 0
Dim ConStr As String = GetConnectionSt ring()
Dim myConnection As New SqlConnection(C onStr)
If Not (myConnection.S tate = ConnectionState .Open) Then
myConnection.Op en()
Dim sSQL As String = "DELETE FROM tblItems" ' Clean out table
before load from text file
Dim myCommand As New SqlCommand(sSQL , myConnection)
RetVal = myCommand.Execu teNonQuery()
Dim ds As New DataSet()
Dim dt As New DataTable("temp ")
Dim da As New SqlClient.SqlDa taAdapter("SELE CT * FROM
tblItems", ConStr)
ds.Clear()
da.FillSchema(d s, SchemaType.Mapp ed, "temp")
Dim path As String = AppPath(True) & "Incoming\OG.tx t"
Dim sR As IO.StreamReader =
System.IO.File. OpenText(path.T oString)
Dim fileline As String = ""
Try
Do While sR.Peek <> -1
fileline = sR.ReadLine
Dim dr As DataRow = ds.Tables("temp ").NewRow
dr("CompanyNumb er") =
Convert.ToInt32 (fileline.Subst ring(0, 4)) ' 0-4
dr("PriceGroup" ) =
Convert.ToInt16 (fileline.Subst ring(5, 7)) '5-11
dr("ItemCategor yCode") =
Convert.ToInt32 (fileline.Subst ring(12, 5)) '12-16
dr("ItemNumber" ) =
Convert.ToInt32 (fileline.Subst ring(17, 9)) '17-25
dr("ItemDescrip tion") = fileline.Substr ing(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.WriteLi ne("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
4348
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 code and text file included. Please help Regards
8
3630
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 text fields. Each date for example 10/31/03 or October 31st 2003 is stored as 10/31/A3 in the system. My reasoning for this is because they couldn't solve their Y2K problem or this is their solution to it. All dates prior to 2000 are stored...
11
19862
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 when it all tried to go in it bombed. So I need to write this to a tab delimited file and I sure hope somebody is awake tonight. How do I write my dataset to make a tab delimted text file?
4
12789
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
12
2896
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 workstation it works fine, but when I publish the page on our DEV server it doesn't fine the CSV file from the client. Has anyone done this before? If so, how do I do it? I'm new to ASP.net so
13
2802
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 into a textbox, or upload it (haven't decided yet). The problem I have is that the text file consists of around 3000 lines, and I want to display the formatted columns to the user before submitting it to the database (perhaps allowing them to...
2
3375
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 table today, it is linked as a text(255) field, ditto for the first table if I link it today if I link the 2nd table using access2003 (and the same odbc data source) it is linked as a memo field
9
6975
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 population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference, but the other items are passed normally.
0
2721
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, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT Product.ID, Product.Description, Quantity.Quantity, Quantity.SeialNo, Quantity.SupplierID,...
0
8608
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9086
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8803
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8806
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6459
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5820
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.