Results 1 to 9 of 9

Thread: [RESOLVED] Recordset equivalent in VB.NET

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Resolved [RESOLVED] Recordset equivalent in VB.NET

    In VB6 there was a thing called a Recordset Object. I could start with a stored procedure which returned some columns and rows from a SELECT statement, and then get the .Items(X) from the Recordset in code.

    I am trying the same thing in .NET but cant find a Recordset object or anything like it. I have seen alot of references to the Dataset, but nearly all the examples are binding it to GUI objects. Thats not what I want.

    I have stored procs that return "recordsets". How can I get at them from VB.NET?

    Here is how I am currently getting return values from SP to .NET:

    VB Code:
    1. Dim myGetInfoConnection As New SqlConnection(System.Environment.GetEnvironmentVariable("RhinoConnectionString"))
    2.             Dim myGetInfoCommand As New SqlCommand("spIAS_GetImageFromOperation", myGetInfoConnection)
    3.             '
    4.             ' Mark the Command as a SPROC
    5.             myGetInfoCommand.CommandType = CommandType.StoredProcedure
    6.             '
    7.             ' Add Parameters to SPROC
    8.             prmRETURN_VALUE = myGetInfoCommand.Parameters.Add("@RETURN_VALUE", SqlDbType.Int)
    9.             prmRETURN_VALUE.Direction = ParameterDirection.ReturnValue
    10.             '
    11.             Dim prmOperationID As New SqlParameter("@operationID", SqlDbType.Int)
    12.             prmOperationID.Value = m_OperationID
    13.             myGetInfoCommand.Parameters.Add(prmOperationID)
    14.             '
    15.             Try
    16.                 '
    17.                 myGetInfoConnection.Open()
    18.                 myGetInfoCommand.ExecuteNonQuery()
    19.                 m_PictureID = myGetInfoCommand.Parameters(0).Value
    20.                 '
    21.                 myGetInfoConnection.Close()
    22.                 '
    23.             Catch SQLexc As SqlException
    24.                 Response.Write("SP Failed. Error Details are: " & SQLexc.ToString())
    25.             End Try

    Thanks!

    Dave
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  2. #2
    Lively Member
    Join Date
    Nov 2005
    Location
    Bristol/Bath
    Posts
    85

    Re: Recordset equivalent in VB.NET

    THe .net equivalent is the dataset and you use a data adapter between your database and your dataset. Heres a quick tutorial

    http://www.homeandlearn.co.uk/NET/nets12p4.html
    VS.Net Pro 2003, .NET 1.1

    My Photography portfolio:
    http://www.ephotozine.com/user.cfm?user=24834

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Recordset equivalent in VB.NET

    We have this CLASS:

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Data
    5. Imports System.Data.SqlClient
    6.  
    7. Public Class Database
    8.  
    9.     Private Connection As SqlClient.SqlConnection
    10.     Private Command As SqlClient.SqlCommand
    11.     Private Parameters As SqlClient.SqlParameterCollection
    12.     Private strServerSelected As String
    13.     Private strDatabaseSelected As String
    14.     Private strConnectionString As String
    15.  
    16.     Public Function EstablishConnect(ByVal strServer As String, ByVal strDatabase As String, ByVal booLeaveOpen As Boolean) As Boolean
    17.  
    18.         strConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=" & strDatabase _
    19.                         & ";server=" & strServer & ";Connect Timeout=30"
    20.  
    21.         Connection = New SqlClient.SqlConnection
    22.         Connection.ConnectionString = strConnectionString
    23.  
    24.         Call OpenConnection()
    25.  
    26.         If Not booLeaveOpen Then Call CloseConnection()
    27.  
    28.     End Function
    29.  
    30.     Public Function OpenConnection() As Boolean
    31.         Connection.Open()
    32.     End Function
    33.  
    34.     Public Function CloseConnection() As Boolean
    35.         Connection.Close()
    36.     End Function
    37.  
    38.     Public Function SetSProc(ByVal strSP As String) As Boolean
    39.         Command = New SqlClient.SqlCommand
    40.         Try
    41.             Command.Connection = Connection
    42.             Command.CommandType = CommandType.StoredProcedure
    43.             Command.CommandText = strSP
    44.         Catch Exp As SqlException
    45.             MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
    46.  
    47.         Catch Exp As Exception
    48.             MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
    49.  
    50.         End Try
    51.  
    52.     End Function
    53.  
    54.     Public Function SetParam(ByVal strParam As String, ByVal lngLen As Integer, ByVal strValue As String) As Boolean
    55.         Dim parameter As New SqlParameter
    56.         parameter = Command.Parameters.Add(strParam, Data.SqlDbType.VarChar, lngLen)
    57.         parameter.Value = strValue
    58.     End Function
    59.  
    60.     Public Function ExecuteSProc() As SqlDataReader
    61.  
    62.         ExecuteSProc = Nothing
    63.  
    64.         Try
    65.             ExecuteSProc = Command.ExecuteReader
    66.         Catch Exp As SqlException
    67.             MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
    68.  
    69.         Catch Exp As Exception
    70.             MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
    71.  
    72.         End Try
    73.  
    74.     End Function
    75. End Class
    and use it this way...

    VB Code:
    1. db = New Database
    2.         Dim dr As SqlClient.SqlDataReader
    3.         'Was me.txtServer.text
    4.         db.EstablishConnect(Me.txtServer.Text, ADC.strDB, True)
    5.  
    6.         db.SetSProc("GetADCReportNames_P")
    7.         dr = db.ExecuteSProc()
    8.  
    9.         cmbReports.SelectedIndex = -1
    10.         cmbReports.Items.Clear()
    11.  
    12.         Do While dr.Read()
    13.             cmbReports.Items.Add(dr.Item(1))
    14.             cmbReports.SelectedIndex = 0
    15.         Loop
    16.  
    17.         db.CloseConnection()
    or this way

    VB Code:
    1. db.OpenConnection()
    2.             db.SetSProc("GetADCReportSpecs_P")
    3.             db.SetParam("@SP", 50, cmbReports.SelectedItem)
    4.  
    5.             dr = db.ExecuteSProc()
    6.  
    7.             Do While dr.Read
    8.                 RO.CreateObject(dr.Item(1), dr.Item(2), dr.Item(3) _
    9.                         , dr.Item(4), dr.Item(5), dr.Item(6) _
    10.                         , dr.Item(7), dr.Item(8) & "")
    11.             Loop
    I'm still new to .net - but this works for me...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Recordset equivalent in VB.NET

    Also, there is the datareader, which is a forward only recordset. This will be faster than a dataset if you only need to read through the set one time.
    My usual boring signature: Nothing

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Recordset equivalent in VB.NET

    As you can see I use the datareader - we like forward-only, read-only type recordsets...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Recordset equivalent in VB.NET

    Thanks all for this. sz, i dont get this loop:
    VB Code:
    1. Do While dr.Read
    2.                 RO.CreateObject(dr.Item(1), dr.Item(2), dr.Item(3) _
    3.                         , dr.Item(4), dr.Item(5), dr.Item(6) _
    4.                         , dr.Item(7), dr.Item(8) & "")
    5.             Loop
    What is the RO? What is the deal using CreateObject?
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Recordset equivalent in VB.NET

    RO is just another CLASS object that I am loading the data into - doesn't have anything to do with the DATABASE object or my example - just what I am doing with my recordset data...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: Recordset equivalent in VB.NET

    Couple of issues I see here:

    1) Your use of variable names is awkward, some are keywords
    2) Your parameters object only takes varchar
    3) Your connection remains open across the entire time or lifetime of the project...it might be best to close the connection after executing a command object. (I know you have a function to close the connection, but you should not rely on programmers to use that class if they have to call that function many time, I guess it really depends on the complexity of the program (maybe your app was small and an open connection wasn't a bad idea ??))
    4) You should not store your string file in the actual class, wouldn't the application configuration file be a better choice ?
    5) Ugh @ dynamically changing the connection string via variables
    6) Did you know datareaders maintain the connection to the db? Performance loss
    7) I do not see any finally statements to release the command object / the connection object or your reader out of memory, big time memory leak (the GC will collect the memory only when it deems necessary
    8) Some of your functions could be GET / SET properties rather than actual function definitions.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Recordset equivalent in VB.NET

    Thanks for the info! This was kind of a learning experience for us in the whole .Net and OO world - thanks for the constructive criticism. Always appreciated!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width