Results 1 to 9 of 9

Thread: Record Set (Databases) [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166

    Record Set (Databases) [RESOLVED]

    I have the following code and I need help with recordset. How do I retireve the data??? Thanks.

    Code:
    			string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data.mdb";
     			
    			OleDbConnection connDatabase = new OleDbConnection(strConn); 
    			connDatabase.Open();
    			string DatQuery;
    
    
    			DatQuery = "[Name] = '" + cboData.Text.Trim() + "' OR [Output] = '" 
    			+ cboData.Text.Trim() + "'";
    Last edited by jordan23; Jul 21st, 2004 at 09:06 AM.

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    one way
    Code:
    OleDbDataReader myDataReader = null;
    
        OleDbConnection myOleDbConnection = new OleDbConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind;provider=sqloledb");
        OleDbCommand myOleDbCommand = new OleDbCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", myOleDbConnection);
    
        try
        {
          myOleDbConnection.Open();
          myDataReader = myOleDbCommand.ExecuteReader();
    
          Console.Write("EmployeeID" + "\t");
          Console.Write("Name" + "\t");
          Console.Write("Title" + "\t");
          Console.Write("ReportsTo" + "\n");
    
          // Always call Read before accessing data.
          while (myDataReader.Read())
          {
            Console.Write(myDataReader.GetInt32(0) + "\t");
            Console.Write(myDataReader.GetString(2) + " " + myDataReader.GetString(1) + "\t");
            Console.Write(myDataReader.GetString(3) + "\t");
            if (myDataReader.IsDBNull(4))
              Console.Write("N/A\n");
            else
              Console.Write(myDataReader.GetInt32(4) + "\n");
          }
        }
        catch(Exception e)
        {
          Console.Write(e.ToString());
        }
        finally
        {
          // Always call Close when done reading.
          if (myDataReader != null)
            myDataReader.Close();
    
          // Close the connection when done with it.
          if (myOleDbConnection.State == ConnectionState.Open)
            myOleDbConnection.Close();
        }

  3. #3

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    I don't really understand your code. Is there a way to use my but just add the read part?? I am a newbie. sorry.

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    put your query into the OleDBCommand object
    Code:
    OleDBCommand cmd = new OleDBCommand(DatQuery,connDatabase);
    then create an OleDBDataReader to read the results
    Code:
    OleDbDataReader reader = cmd.ExecuteReader();
    you can now loop thru this reader to get your results
    Code:
    while(reader.Read())
     {
       Console.WriteLine( reader[0].ToString());
     }
     //close reader
     reader.Close();

  5. #5

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    I am getting these errors -

    (947): The type or namespace name 'OleDBCommand' could not be found (are you missing a using directive or an assembly reference?)

    (948): The type or namespace name 'cmd' could not be found (are you missing a using directive or an assembly reference?)

  6. #6
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    make sure you have the following:
    Code:
    using System;
    using System.Data;
    using System.Data.OleDb;
    and check the case OleDbCommand as opposed to OleDBCommand

  7. #7

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    It was the case sensitive part. I am having problems with my SQL statement but I am working on that. Thanks again for your help.

  8. #8
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    no problems,
    with regards to getting your sql right, before sending it to be processed write it to the screen so you can see if it looks right.

  9. #9

    Thread Starter
    Addicted Member jordan23's Avatar
    Join Date
    Dec 2002
    Posts
    166
    I used your advice and got it work perfectly!!! Thanks again.

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