vdiaz761
asked on
Function To Connect To Database ASP.NET VB
Hello Experts,
I have the following code:
Function connectToDB(ByVal strQuery As String) As OleDbDataReader
Dim connDb As OleDbConnection
Dim cmdParamQuery As OleDbCommand
Dim dtrDataReader As OleDbDataReader
Dim datasource As String = (Server.MapPath(System.Con figuration .Configura tionManage r.AppSetti ngs("conSt ring"))).T oLower()
connDb = New OleDbConnection("PROVIDER= Microsoft. Jet.OLEDB. 4.0;DATA SOURCE=" + datasource)
connDb.Open()
cmdParamQuery = New OleDbCommand(strQuery, connDb) 'strQuery PASSED IN FUNCTION CALL
dtrDataReader = cmdParamQuery.ExecuteReade r()
Return dtrDataReader
End Function
Sub displayData()
Dim dtrNewsArticles As OleDbDataReader
Dim strQuery As String = "SELECT * FROM tableName"
dtrNewsArticles = connectToDB(strQuery)
While dtrNewsArticles.Read()
'Do Code
End While
'Close Database
End Sub
My question is, how would I close the database after ther WHile Loop?
Thanks
Vic
I have the following code:
Function connectToDB(ByVal strQuery As String) As OleDbDataReader
Dim connDb As OleDbConnection
Dim cmdParamQuery As OleDbCommand
Dim dtrDataReader As OleDbDataReader
Dim datasource As String = (Server.MapPath(System.Con
connDb = New OleDbConnection("PROVIDER=
connDb.Open()
cmdParamQuery = New OleDbCommand(strQuery, connDb) 'strQuery PASSED IN FUNCTION CALL
dtrDataReader = cmdParamQuery.ExecuteReade
Return dtrDataReader
End Function
Sub displayData()
Dim dtrNewsArticles As OleDbDataReader
Dim strQuery As String = "SELECT * FROM tableName"
dtrNewsArticles = connectToDB(strQuery)
While dtrNewsArticles.Read()
'Do Code
End While
'Close Database
End Sub
My question is, how would I close the database after ther WHile Loop?
Thanks
Vic
ASKER
Lem,
Thanks for the quick reply. Two things however:
I was trying to avoid having to Dim the OleDbConnection and I get the following error with your suggestion:
Object reference not set to an instance of an object.
This error is in reference to connDb.Close()
Thanks
Vic
Thanks for the quick reply. Two things however:
I was trying to avoid having to Dim the OleDbConnection and I get the following error with your suggestion:
Object reference not set to an instance of an object.
This error is in reference to connDb.Close()
Thanks
Vic
ASKER
Lem,
Also, your suggestion to use a dataset... can you explain?
Vic
Also, your suggestion to use a dataset... can you explain?
Vic
Public Function DevolverDataset(ByVal myExecuteQuery As String) As DataSet
Dim MyConnectionString As String = System.Configuration.Confi gurationSe ttings.App Settings(" conn_peic. Connection String")
Dim myConnection As New System.Data.OleDb.OleDbCon nection(My Connection String)
myConnection.Open()
Dim dataadapter_PBQ As New OleDb.OleDbDataAdapter(myE xecuteQuer y, myConnection)
Dim dataset_PBQ As New DataSet
dataadapter_PBQ.Fill(datas et_PBQ)
myConnection.Close()
Return dataset_PBQ
End Function
This Function returns Dummy Dataset, that bounds with any VB.NET controls.
Dim MyConnectionString As String = System.Configuration.Confi
Dim myConnection As New System.Data.OleDb.OleDbCon
myConnection.Open()
Dim dataadapter_PBQ As New OleDb.OleDbDataAdapter(myE
Dim dataset_PBQ As New DataSet
dataadapter_PBQ.Fill(datas
myConnection.Close()
Return dataset_PBQ
End Function
This Function returns Dummy Dataset, that bounds with any VB.NET controls.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what do I need to import for DataSet (i.e Imports System.Data.OleDb)
System.Data
ASKER
Great... Almost there. How do I now access the columns return from the query in my displayData subRoutine as I would have I used a datareader:
ie dataReader("columnName")
Thanks
ie dataReader("columnName")
Thanks
for each dr As DataRow in ds.Tables(0).Rows
dr("columnName") 'with this you'll get the column value in the actual row
next
dr("columnName") 'with this you'll get the column value in the actual row
next
ASKER
Perfect! You da man! Thanks...
ASKER
Lem...
Are you ready?
Isaiah Chapter 13
Are you ready?
Isaiah Chapter 13
sure man... but still i have to live in this planet :)
first i suggest you use an dataset in thi case...
and for your problem you'll need to pass the connection to the funciton too:
Function connectToDB(ByVal strQuery As String, ByVal connDb As OleDbConnection) As OleDbDataReader
Dim cmdParamQuery As OleDbCommand
Dim dtrDataReader As OleDbDataReader
Dim datasource As String = (Server.MapPath(System.Con
connDb = New OleDbConnection("PROVIDER=
connDb.Open()
cmdParamQuery = New OleDbCommand(strQuery, connDb) 'strQuery PASSED IN FUNCTION CALL
dtrDataReader = cmdParamQuery.ExecuteReade
Return dtrDataReader
End Function
Sub displayData()
Dim connDb As OleDbConnection
Dim dtrNewsArticles As OleDbDataReader
Dim strQuery As String = "SELECT * FROM tableName"
dtrNewsArticles = connectToDB(strQuery, connDb)
While dtrNewsArticles.Read()
'Do Code
End While
connDb.Close()
End Sub