CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Dec 2002
    Location
    England
    Posts
    17

    database queries in vb.net

    I'm trying, unsucessfully mind you, to implement something that does the following in vb.net, connected to an access database this is my kind of (part psudo) code. any ideas about how i get this to work?
    Code:
    dim count as integer
    dim r as integer
    dim entrySel as arraylist
    dim dbsize as double
    
    count = 0
    dbsize = result of "select count from table"
    
    while count < dbsize
        r = int(rnd()*dbsize)
        
    if entrysel.contains(r)=false
    then
    entrysel.add(r)
    
    use "select * from table where id = 'r' "
    and add this result to the datagrid/dataset
    
    count++
    end if 
    end while
    or something like that, basically capable of the same thing. select a user specified number of items from a database at random, with no repettionn.
    (Sorry my spelling is so bad today)

    thanks in advance
    Last edited by xeviva; March 17th, 2003 at 12:02 PM.

  2. #2
    Join Date
    Feb 2003
    Posts
    51
    Is there any particular place where you seem to be getting stuck.

  3. #3
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    sql queries, i think my syntax is screwed to start with, and filling the data set in this manner

  4. #4
    Join Date
    Feb 2003
    Posts
    51
    Is there a specific error that you are getting... or are you just not getting any results?

  5. #5
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    not sure how to do a query, that maybe returns 1 result, put it into a results set, then do another query, adding that result to the first one and so on.

    I don't know how to code this properly at all. i can do the logic, but not the proper code.

    Can you help me code it?

  6. #6
    Join Date
    Feb 2003
    Posts
    51
    Well, here is some code for querying data. This particular one should work with ODBC-based databases like Access.

    ----------------------------------------------------------------------------------

    Shared Function QueryData(ByVal DBConnString As String, ByVal queryString As String) As System.Data.DataSet

    Dim DBConn As New Data.OleDb.OleDbConnection(DBConnString)
    Dim DBSelectCommand As New Data.OleDb.OleDbCommand(queryString, DBConn)
    Dim ds As New Data.DataSet()
    Dim adapter As New Data.OleDb.OleDbDataAdapter()

    Try
    DBConn.Open()
    adapter.SelectCommand = DBSelectCommand
    adapter.Fill(ds)
    DBConn.Close()
    Return ds
    Catch ex As Data.OleDb.OleDbException
    MsgBox(ex.ToString())
    End Try

    End Function

    ------------------------------------------------------------------------------

    Basically, the idea is this:

    First, you want to create a database connection object, a dataset object, and a database adapter object. The connection object handles the process of connecting to the database, the adapter handles the transfer of data between the program and the database, and the dataset holds your query results.

    As you can see, the database connection object has a connection string associated with it. This string basically tells the connection where the database is... for example, for Access databases, you would pass the physical location of the database file along with the type of database you're connecting to.

    Second, you create your database query command. For simple queries, this is where you would put your SQL code. As you can see, this takes the connection object and the query SQL code as arguments.

    Finally, you run through the process of actually fill the dataset. You do this by opening the connection, setting the adapter's SelectCommand property to the database query command you created, running the adapters Fill method (using your dataset as an argument... this is what will get filled), and finally closing the database connection. You can also see that I put this in a Try-Catch statement because you can never tell what kind of database errors you might get (mostly related to incorrect SQL syntax, missing columns, missing records, and the like).

    You should probably look at data adapters if you want to do more complicated things like adding tables to your datasets and handling updates and inserts.

  7. #7
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    ok thanks so far,

    what is the exact syntax for the sql queries? As every time I try to do anything other than select * from table, I fall into my catch block of code, so I'm assuming there's something wrong with the way I'm setting out the queries?

    When I try to "select count from table" I get a responce of 0 where there are 50 entries in the database, why is this happening and how can I correct it?

  8. #8
    Join Date
    Feb 2003
    Posts
    51
    What kind of error are you getting from the Catch block?

  9. #9
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    Try

    select count(*) from table
    instat of
    select count from table

    Danny

  10. #10
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    cheers DdH

  11. #11
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    the error message out put by the catch block is huge! far too much to reproduce, but it seems to be a problem mostly with the data adapter. and with the sql queries second. I'll have to recheck the data adapter settings, maybe i've done something stupid somewhere.

    I still can't get the correct result to select count(*) from table, it still tells me 0, where I know it should be 50. I can get the data adapter to preview the data, and it finds 50 records in the table, but counts 0?

  12. #12
    Join Date
    Feb 2003
    Posts
    51
    I would suggest you run through the code line by line using breakpoints and the de-bugger and see exactly where the code is failing. If it is only coming out of the catch block, then it's a database issue. Anywhere else, and it's a code issue.

    As for the SQL... what happens when you run the same code outside of VB? Does the same query run okay in the database you're using?

  13. #13
    Join Date
    Dec 2002
    Location
    England
    Posts
    17
    the way i write it in Access it works, I would write sql like that in oracle sql databases and it would be fine. I'm guessing that it has to be phrased differently to what i'm doing?

    Code:
    DataAdapter.SelectCommand.CommandText = "Select * From table Where Item ID = '2'"

  14. #14
    Join Date
    Feb 2003
    Posts
    51
    Try this in VB:

    DataAdapter.SelectCommand.CommandText = "Select * From table Where Item ID = ""2"""

    VB is very particular about quotes in SQL statements. I've had that issue myself.

  15. #15
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189
    You also might try
    Code:
    DataAdapter.SelectCommand.CommandText = "Select * From table Where [Item ID] = '2'"
    The space in 'Item ID' could be giving you a problem

Page 1 of 2 12 LastLast

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

Featured