Solved

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

Posted on 2004-10-18
3
835 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

14 Experts available now in Live!

Get 1:1 Help Now