Solved

Recordset in VB6, and what is the equivalent in VB.Net?

Posted on 2004-10-18
3
849 Views
Last Modified: 2010-05-18
Hi,

  I used to use recordsets a lot in VB6. Now I wish to know if there's any way to code in VB.Net using SQLDataReader/SQLDataAdapter/DataSet to perform likke recordsets (especially opening a few recordsets to perform certain tasks)?

Example:

I have table_a, table_b, table_c, table_d

table_a is the mail table whilst the rest are the reference tables.

table_a = transactions (custno, userno, transtime, billno, charges)
table_b = seqnotable (fileno, curr, skip, prefix)
table_c = billss (billno, custno, totalcharges)
table_d = qlist (custno, billno, posted)

main():
do while not (bof(qlist) or eof(qlist))
  if anytransaction(custno)
    if qlist.billno = '' or qlist is null then
      newbillno = createbill(custno)
    else
      newbillno = qlist.billno
    endif
    update transactions.billno with newbillno for each userno under that custno
    totalcharges = calculate total charges in transaction for that newBillno  
    update bills.totalcharges with totalcharges for that billno
    update qlist.posted = 'Y' for that custno + billno
  else
    update qlist.billno = 'NIL' for that custno
  endif
  move to next record in qlist
loop


function anytransaction(byval custno) as boolean
  select count(*) in transaction for that custno
  if results <= 0 records
    anytransaction = False
  Else
    anytransaction = true
  endif
end function

function createbill(byval custno) as string
  look for the fileno = 'bill' in seqnotable,
  get the seqnotable.curr
 
  newBillNo = prefix + cstr(seqnotale.curr)
 
  update seqnotable set curr= curr + skip for this fileno = 'bill'

  createbill = newNillNo
end function

Basically this is the frame of the program. Since there are reference tables and main table and a lot of updates here and there in the tables, I use recordser to represent each of the table whenever they are needed. VB6 allows opening of a number of recordsets at the same time with the conditions that they are of different names.

No I have to rewrite the same program in VB.Net, I am not sure how should I begin with it. I tried DataSet but I have some problems when doing the updates to the actual database. The update commands seems to give me some problems when I use CommandBuilder.

I use System.Data.SQLClient because I connect to SQL Server as database.

Someone there please help me.. Thank you!
0
Comment
Question by:allicia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

by:J_Mak
J_Mak earned 35 total points
ID: 12337258
Have a look at this tutorial:

http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/adoplusoverview.aspx

You should use datasets which is .NET idea of creating a 'disconnected' environment for data access. That is, you do not explicitly have to be connected to the underlying back end database to access your information. It's a great tutorial.
0
 
LVL 27

Accepted Solution

by:
planocz earned 45 total points
ID: 12339095
Here is a small sample ......

Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents dgContacts As System.Windows.Forms.DataGrid

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.dgContacts = New System.Windows.Forms.DataGrid()
        CType(Me.dgContacts, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'dgContacts
        '
        Me.dgContacts.DataMember = ""
        Me.dgContacts.Dock = System.Windows.Forms.DockStyle.Fill
        Me.dgContacts.Name = "dgContacts"
        Me.dgContacts.Size = New System.Drawing.Size(416, 205)
        Me.dgContacts.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 205)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dgContacts})
        Me.Name = "Form1"
        Me.Text = "UseDataAdapter"
        CType(Me.dgContacts, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Const SELECT_STRING As String = _
        "SELECT * FROM Customers ORDER BY CompanyName, CustomerID"

    'REPLACE SQLDESKTOP with your SQL Server Name on your PC.
    Private Const CONNECT_STRING As String = _
        "Data Source=(local)\SQLDESKTOP;Initial Catalog=Customers;User Id=sa"

    Protected Const MSDE_CONNECTION_STRING As String = _
      "Server=(local)\SQLDESKTOP;" & _
      "DataBase=Northwind;" & _
      "Integrated Security=SSPI"

    ' The DataSet that holds the data.
    Private m_DataSet As DataSet

    ' Load the data.
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim data_adapter As SqlDataAdapter

        ' Create the SqlDataAdapter.
        data_adapter = New SqlDataAdapter(SELECT_STRING, MSDE_CONNECTION_STRING) 'CONNECT_STRING)

        ' Map Table to Contacts.
        data_adapter.TableMappings.Add("Table", "Customers")

        ' Fill the DataSet.
        m_DataSet = New DataSet()
        data_adapter.Fill(m_DataSet)

        ' Bind the DataGrid control to the Contacts DataTable.
        ' Changed by Jim Rogers (jrogers@antaresnet.com)
        dgContacts.SetDataBinding(m_DataSet, "Customers")    '<----customers is the table that you pull from
        'dgContacts.DataSource = m_DataSet.Tables("Contacts") '<----you can multible tables inside one dataset
    End Sub

    ' Save any changes to the data.
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If m_DataSet.HasChanges() Then
            Dim data_adapter As SqlDataAdapter
            Dim command_builder As SqlCommandBuilder

            ' Create the DataAdapter.
            data_adapter = New SqlDataAdapter(SELECT_STRING, CONNECT_STRING)

            ' Map Table to Contacts.
            data_adapter.TableMappings.Add("Table", "Customers")

            ' Make the CommandBuilder generate the
            ' insert, update, and delete commands.
            command_builder = New SqlCommandBuilder(data_adapter)

            ' Uncomment this code to see the INSERT,
            ' UPDATE, and DELETE commands.
            'Debug.WriteLine("*** INSERT ***")
            'Debug.WriteLine(command_builder.GetInsertCommand.CommandText)
            'Debug.WriteLine("*** UPDATE ***")
            Debug.WriteLine(command_builder.GetUpdateCommand.CommandText)
            'Debug.WriteLine("*** DELETE ***")
            'Debug.WriteLine(command_builder.GetDeleteCommand.CommandText)

            ' Save the changes.
            data_adapter.Update(m_DataSet)
        End If
    End Sub
End Class
0
 

Author Comment

by:allicia
ID: 12367847
This will take time to digest...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 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