|
-
Jul 21st, 2004, 07:23 AM
#1
Thread Starter
Addicted Member
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.
-
Jul 21st, 2004, 07:43 AM
#2
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();
}
-
Jul 21st, 2004, 07:49 AM
#3
Thread Starter
Addicted Member
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.
-
Jul 21st, 2004, 08:01 AM
#4
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();
-
Jul 21st, 2004, 08:09 AM
#5
Thread Starter
Addicted Member
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?)
-
Jul 21st, 2004, 08:39 AM
#6
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
-
Jul 21st, 2004, 09:06 AM
#7
Thread Starter
Addicted Member
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.
-
Jul 21st, 2004, 09:13 AM
#8
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.
-
Jul 21st, 2004, 10:43 AM
#9
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|