Re: reading values from database

From: Robin Tucker (idontwanttobespammedanymore_at_reallyidont.com)
Date: 10/04/04


Date: Mon, 4 Oct 2004 15:36:35 +0100

Ahh yes. My connection gets closed a little later, ie. like this (not made
clear in my original post)

Dim theObject as MyDataBaseClass()

Try

    ' Create it

    theDBObject = new MyDataBaseClass ( s_ConnectionString )

    ' Execute a method...

    Result = theDBObject.FetchMyImagesFromDataBase()
    If Not Result Then
        Throw New Exception ( "Something screwed up" )
    End If

    ' and another one

    Result = theDBObject.DoAnotherThingBeforeClosing()

    ...
    ...

Catch Ex as Exception

Finally

    If not theDBObject is Nothing Then
        theDBObject.Close()
    End If

End Try

"W.G. Ryan eMVP" <WilliamRyan@gmail.com> wrote in message
news:%23uhhNmhqEHA.536@TK2MSFTNGP11.phx.gbl...
> Robin:
>
> Just to add a quick tidbit - closing the Connection is ostensibly one of
> the
> more important issues you need to address. You can specify
> CloseCOnnection
> in the CommandBehavior Enumeration of ExecuteReader which will handle this
> for you whenever you close the reader but it's a must unless you want to
> risk some serious performance hits. I'd also avoid trapping
> System.Exception here. A StackOVerflow isn't the same things as a
> Connection Timeout so I'd trap Connection.Open() individually as well as
> executeReader and wrap them in SqlException, OleDbException etc
>
> --
> W.G. Ryan, MVP
>
> www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
> "Robin Tucker" <idontwanttobespammedanymore@reallyidont.com> wrote in
> message news:cjrjc7$qc$1$8300dec7@news.demon.co.uk...
>> I do something like this. Modify CommandType and CommandText for your
> query
>> (I use stored procedures here).
>>
>> ' Our data reader
>>
>> Dim DataReader As SqlDataReader
>>
>> ' Result
>>
>> Dim Result As Boolean = False
>>
>> ' Do sommit
>>
>> Try
>>
>> Dim Command As New SqlCommand
>>
>> ' Setup the command object
>>
>> Command.Connection = m_Connection
>> Command.CommandText = "proc_SomeStoredProcedure_or_select_text"
>> Command.CommandType = CommandType.StoredProcedure
>>
>> ' Execute the reader
>>
>> DataReader = Command.ExecuteReader()
>>
>> ' Now iterate the result set
>>
>> If DataReader.HasRows Then
>>
>> While DataReader.Read() ' ie. for each record returned
>>
>> ' Get a value...
>>
>> Dim aValue, aNotherValue as integer
>>
>> aValue = DataReader.GetInt32(0)
>> aNotherValue = DataReader.GetInt32(1)
>>
>> End While
>>
>> End If
>>
>> ' Success
>>
>> Result = True
>>
>> Catch Ex As Exception
>>
>> ' Failed
>>
>> Result = False
>>
>> Finally
>>
>> ' Close reader
>>
>> If Not DataReader Is Nothing Then
>>
>> DataReader.Close()
>>
>> End If
>>
>> End Try
>>
>> Return Result
>>
>> "simon" <simon.zupan@stud-moderna.si> wrote in message
>> news:uPuKMIhqEHA.1308@TK2MSFTNGP14.phx.gbl...
>> What is the best way to read the values from the datatbase.
>>
>> I have sql="SELECT userName, userCountry, userVisit from users where
>> userID=2"
>>
>> Create a command object?
>>
>> Dim oCmd As SqlCommand
>> oCmd = New SqlCommand(sql, Connection)
>> object = oCmd.ExecuteScalar()
>> oCmd.Connection.Close()
>> How can I read the values from this?
>>
>> userName=object.fields(0).Value ' or something like that
>> userCountry=...
>> userVisit=....
>>
>> Thank you,
>> Simon
>>
>>
>
>



Relevant Pages

  • Re: reading values from database
    ... Just to add a quick tidbit - closing the Connection is ostensibly one of the ... for you whenever you close the reader but it's a must unless you want to ... > Dim DataReader As SqlDataReader ... Dim Command As New SqlCommand ...
    (microsoft.public.dotnet.languages.vb)
  • Re: reading values from database
    ... My connection gets closed a little later, ... > Dim theObject as MyDataBaseClass ... >> for you whenever you close the reader but it's a must unless you want to ... Dim Command As New SqlCommand ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Closing a connection returned from separate class
    ... I am not fond of the "return a connection" type of architecture, ... The command button eventually calls a SP that has a parameter @ClientName. ... Public Function ConnectToDBAs SqlConnection ... Dim strConnString As String ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: SqlDataReader Function From a VB6 refugee
    ... the SQL and a connection and it works. ... Dim SQLConn As New SqlConnection ... Dim iDr As SqlDataReader = ... After you return the data reader you are closing the connection: ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: newbie; Update not working
    ... The first command is working ok I get the accountbalance and am able to add ... StudentID, FName, LName, MajorID, AccountBalance, BookStoreEmp. ... Is there more than just opening the connection and running the Sql ... >> Dim dsCurrent As New DataSet ...
    (microsoft.public.dotnet.framework.adonet)