Re: reading values from database
From: Robin Tucker (idontwanttobespammedanymore_at_reallyidont.com)
Date: 10/04/04
- Next message: CJ Taylor: "Re: The thread '<No Name>' (0x29c) has exited with code 0 (0x0)"
- Previous message: Jan Hyde: "Re: How to display/ change icons in listbox"
- In reply to: W.G. Ryan eMVP: "Re: reading values from database"
- Next in thread: W.G. Ryan eMVP: "Re: reading values from database"
- Reply: W.G. Ryan eMVP: "Re: reading values from database"
- Messages sorted by: [ date ] [ thread ]
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
>>
>>
>
>
- Next message: CJ Taylor: "Re: The thread '<No Name>' (0x29c) has exited with code 0 (0x0)"
- Previous message: Jan Hyde: "Re: How to display/ change icons in listbox"
- In reply to: W.G. Ryan eMVP: "Re: reading values from database"
- Next in thread: W.G. Ryan eMVP: "Re: reading values from database"
- Reply: W.G. Ryan eMVP: "Re: reading values from database"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|