Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / January 2006

Tip: Looking for answers? Try searching our database.

Sql Sevrer not updated after copying DataRows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brandon Olson - 10 Jan 2006 16:12 GMT
Hello all,

I am trying to import a tab delimited text file with 80 columns and 1700
records into a SQLExpress database using vb.net.  Here's how I am doing it:

Dim i as Integer = 0 ' Counter
Dim da as New OleDbDataAdapter("SELECT * FROM [format1.txt]",
textFileConnectionString)
dim dt as new DataTable("FromTable")
dim sqlDt as new dataTable("ToTable")
Dim myDataRow as DataRow
da.Fill(dt)
dim sqlDa as new SqlDataAdapter("SELECT * FROM toTable"),
sqlConnectionString)
sqlDa.Fill(sqlDT)
For each myDataRow in dt.Rows
   sqlDT.Rows.Add(dt.Rows(i).ItemArray)
   'I use this to see if the data was actually written to the new Sql table
   ' This actually works
   Console.WriteLine("fromTable Column(0) - " &
dt.Rows(i).Item(0).ToString)
   Console.Writeline("toTable - & Column(0) - " &
sqldt.Rows(i).Item(0).ToString)
   ' I have tried sticking a sqlDT.AcceptChanges() here - doesn't work
   ' I have tried sticking a sqlDA.Update(sqlDT) here - doesn't work
   i+=1
Next

...etc.

This code appears to working, the sqlDT has the new data in it and is
printing to the console, however, when I look in the Server Manager, no data
appears.

What am I missing?

Thanks in advance,

Brandon
Kerry Moorman - 10 Jan 2006 17:10 GMT
Brandon,

You need to use the data adapter to update the database.

The data adapter's Update method is used to update the database with changes
to the in-memory data table.

Kerry Moorman

> Hello all,
>
[quoted text clipped - 35 lines]
>
> Brandon
Brandon Olson - 10 Jan 2006 17:51 GMT
Kerry,

Thanks for your reply.

Does this mean that I cannot use a DataTable and that I have to create an
'Update' stored proc in sql?  I hope there is another way, because this
table has 80 fields and that would be a P.I.T.A.

Thanks again,

Brandon
> Brandon,
>
[quoted text clipped - 48 lines]
>>
>> Brandon
Kerry Moorman - 10 Jan 2006 19:16 GMT
Brandon,

The data adapter's Update method takes the data table as input and applies
the data table changes to the database.

You have to supply valid sql update, insert and delete statements to the
data adapter. One option is to use stored procedures, but you don't have to.

Also, the command builder object may be able to construct the sql update,
insert and delete statements for you.

Kerry Moorman

> Kerry,
>
[quoted text clipped - 59 lines]
> >>
> >> Brandon
Jesús López - 10 Jan 2006 18:04 GMT
Hi Brandon,

This task can be done much easier and much more efficiently using
SqlBulkCopy Class.

Let me give you an example.

Given a tab delimited text file "Contacts.txt" and "Schema.ini" in c:\data:

Schema.ini:

[Contacts.txt]
Format=TabDelimited
Col1=ContactID Long
Col2=FirstName Text Width 50
Col3=LastName Text Width 50

And the following table in SQL Server:

create table Contacts
(
ContactID int primary key,
FirstName varchar(50),
LastName varchar(50)
)

You could use the following code:

   Sub ImportData()
       Dim cnSource As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\data;Extended Properties=Text")
       Dim cmdSource As New OleDbCommand("SELECT ContactID, FirstName,
LastName FROM Contacts#txt", cnSource)
       cnSource.Open()
       Dim reader As OleDbDataReader = cmdSource.ExecuteReader()
       Dim cnDestination As New SqlConnection("Data
Source=(local);Integrated Security=SSPI;Initial Catalog=Pruebas")
       Dim bulkCopy As New SqlBulkCopy(cnDestination)
       bulkCopy.DestinationTableName = "Contacts"
       cnDestination.Open()
       bulkCopy.WriteToServer(reader)
       bulkCopy.Close()
       reader.Close()
       cnSource.Close()
       cnDestination.Close()
   End Sub

This code is several dozens faster than loading the data into a datatable
and using a SqlDataAdapter to insert the data into SQL Server

Regards from Madrid (Spain)

Jesús López
VB MVP

> Hello all,
>
[quoted text clipped - 37 lines]
>
> Brandon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.