Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all. I have been trying to work into converting software from ADO to the much needed ADO.NET. I am stuck on something though.

 

    Public Function adoQuery(ByVal sqlQuery As String, ByVal table As String, Optional ByVal ignore As String = "") As DataSet
       Dim SQLConnection As New System.Data.SqlClient.SqlConnection(connectionString)

       Try
           SQLConnection.Open()

           Dim SQLString As String = sqlQuery
           Dim SQLCommand As New SqlClient.SqlCommand(SQLString, SQLConnection)
           Dim SQLAdapter As New SqlDataAdapter
           SQLAdapter.SelectCommand = SQLCommand
           Dim SQLDataset As New DataSet
           SQLAdapter.Fill(SQLDataset, table)
           Dim SQLDataTable As DataTable = SQLDataset.Tables(table)
           SQLConnection.Close()
           SQLConnection.Dispose()

           adoQuery = SQLDataset
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try
   End Function

 

This function works fine. But, I have to specify the table when I query. With adodb I could just:

 

Dim rs as ADODB.Recordset
rs = query("select * from tblCustomers")

blah = rs("lname").Value

 

But now I have to do something like.

 

Dim rs As New DataSet
rs = adoQuery("select * from tblCustomers", "tblCustomers")

blah = rs.Tables("tblCustomers").Rows(i).Item("lname").ToString()

 

This would be fine except there are allot of very complicated queries where specifying a table just will not work. Is there anyway to get this function to return a dataset structured in a similar way without specifying a table?

 

Thanks in advance for any help.

Posted

I figured it out and here is the answer.

 

    Public Function adoQuery(ByVal sqlQuery As String) As DataSet
       Dim SQLConnection As New System.Data.SqlClient.SqlConnection(connectionString)

       Try
           SQLConnection.Open()

           Dim SQLString As String = sqlQuery
           Dim SQLCommand As New SqlClient.SqlCommand(SQLString, SQLConnection)
           Dim SQLAdapter As New SqlDataAdapter

           SQLAdapter.SelectCommand = SQLCommand

           Dim SQLDataset As New DataSet

           SQLAdapter.Fill(SQLDataset)

           SQLConnection.Close()
           SQLConnection.Dispose()

           adoQuery = SQLDataset
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try
   End Function

Posted
I'm glad that you found a solution. You should also consider using the SqlDataReader for instances where you don't intend on writing any data back to the database.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...