Solved

Copying rows from one table to another in Visual Basic 2010

Posted on 2010-11-25
7
519 Views
Last Modified: 2012-05-10
This is a pointer to question http://www.experts-exchange.com/Microsoft/Development/Q_26626345.html

can anyone help me?

Using Visual Basic 2010 I want to be abe to

1. Filter a table
2. Copy the selected row from one table to another

Please see question for more details
0
Comment
Question by:KCTS
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
pnedic earned 250 total points
ID: 34215899
Hello

If you want to select some row(s) from a table you can use DataSet.Tables(0).Select command:

Dim ds as DataSet = new DataSet()
'...define sql string, make connection to DB, create DataAdapter (as da) and do da.Fill(sql, DBconn)
da = New OleDb.OleDbDataAdapter(sqlStr, liveConnection)
da.Fill(ds)   'populate DataSet with data
Dim whDr() As DataRow
whDr = ds.Tables(0).Select("someColName = "+desiredValue)
Now you got array of rows that satisfies desired condition. Now you can use For loop to position only one row or one column value from one row as:
For i .....
      strValue = CStr(whDr(i).Item(colIndex))
Next

******

To copy one row to the other table, you can use previous logic to get data from onw row and that use INSERT INTO sql command to insert into second table:
INSERT INTO table2 (col1, col2, ... colX) VALUES (val1, val2, ... valX)

Insert command you will execute as :

Dim myTransaction As OleDb.OleDbTransaction
Dim myCommand As New OleDb.OleDbCommand
Try
       myCommand.Connection = liveConnection
       myTransaction = liveConnection.BeginTransaction()
       myCommand.Transaction = myTransaction
       myCommand.CommandText = sqlStr       'sqlStr is Insert sql command
       myCommand.CommandType = CommandType.Text
       myCommand.ExecuteNonQuery()
       myTransaction.Commit()
Catch ex As Exception
       myTransaction.Rollback()
Finally
       myCommand.Dispose()
       myCommand = Nothing
End Try

This is one approach. Other people will probably suggest something else.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 34218484
You can do this

dTable.DefaultView.RowFilter = "Type=1"
Dim dTemp as DataTable = dTable.DefaultView.ToTable()
0
 
LVL 3

Expert Comment

by:amittripathi8
ID: 34222297
just use importrows of datatable method which import rows
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34813026
You should know better being a qualified expert that you are supposed to provide feedback on suggestions.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34879675
Divide between http:# 34215899 and http:#34218484
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now