Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Please Show me how i can search a datatables of mdb files in c# and OLEDB.
Im Just a beginner and i need help in a step by step manner.

Thank You.
Posted
Comments
Jibesh 10-Jan-13 13:04pm    
You will get millions of samples on the internet simply google with keyword 'oledb using C#' I did this for you and see my solution.

 
Share this answer
 
v2
Hi User/Gust,

Please Explain Exact Requirement. I Confuse about your Requirement. So Generally I Suggest Multiple Example or Different Way you can implement your requirement.
First
-----
This program accesses the BugTypes.mdb database, creates a dataset, adds the tables to it, and displays the number of tables, columns, and rows. It also displays the titles of each row.
----------------------------------------------------------------------------------
using System;
using System.Data;
using System.Data.OleDb;
using System.Xml.Serialization;

public class MainClass {
public static void Main ()
{
// Set Access connection and select strings.
// The path to BugTypes.MDB must be changed if you build
// the sample from the command line:
#if USINGPROJECTSYSTEM
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\BugTypes.MDB";
#else
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB";
#endif
string strAccessSelect = "SELECT * FROM Categories";

// Create the dataset and add the Categories table to it:
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}

try
{

OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myAccessConn.Open();
myDataAdapter.Fill(myDataSet,"Categories");

}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}

// A dataset can contain multiple tables, so let's get them
// all into an array:
DataTableCollection dta = myDataSet.Tables;
foreach (DataTable dt in dta)
{
Console.WriteLine("Found data table {0}", dt.TableName);
}

// The next two lines show two different ways you can get the
// count of tables in a dataset:
Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
Console.WriteLine("{0} tables in data set", dta.Count);
// The next several lines show how to get information on
// a specific table by name from the dataset:
Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count);
// The column info is automatically fetched from the database,
// so we can read it here:
Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["Categories"].Columns.Count);
DataColumnCollection drc = myDataSet.Tables["Categories"].Columns;
int i = 0;
foreach (DataColumn dc in drc)
{
// Print the column subscript, then the column's name
// and its data type:
Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType);
}
DataRowCollection dra = myDataSet.Tables["Categories"].Rows;
foreach (DataRow dr in dra)
{
// Print the CategoryID as a subscript, then the CategoryName:
Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]);
}

}
}
----------------------------------------------------------------------------------
Output

The Categories table of the BugTypes.mdb database contains the following information.

Category ID Category Name
----------- -------------
1 Rahul Kumar
2 Abhishek Jaishwal
3 .NET Framework 4.5
4 Windows 8 Super
------------------------------------
Second
-------

This example uses an OleDbDataAdapter to fill a DataTable using an ADO Recordset. This example assumes that you have created an ADO Recordset

OleDbDataAdapter custDA = new OleDbDataAdapter();
DataSet custDS = new DataSet();
DataTable custTable = new DataTable("Customers");
custTable.Columns.Add("CustomerID", typeof(String));
custTable.Columns.Add("CompanyName", typeof(String));
custDS.Tables.Add(custTable);
//Use ADO objects from ADO library (msado15.dll) imported
// as.NET library ADODB.dll using TlbImp.exe
ADODB.Connection adoConn = new ADODB.Connection();
ADODB.Recordset adoRS = new ADODB.Recordset();
adoConn.Open("Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;", "", "", -1);
adoRS.Open("SELECT CustomerID, CompanyName FROM Customers", adoConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
custDA.Fill(custTable, adoRS);
adoRS.Close();
adoConn.Close();
------------------------------------------------------------------------------

Also Some Useful Links :http://en.csharp-online.net/Working_with_Data%E2%80%94Connecting_to_Access_using_OLE_DB[^]

http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-built-in-oledb-csv-parser[^]

http://www.dreamincode.net/forums/topic/33979-oledb-data-objects-in-c%23/[^]

http://www.daniweb.com/software-development/csharp/threads/343466/search-through-data-table[^]
-------------------------------------------------------------------

Further You Can Conversation with more Details.
Rahul Kumar,
rahulinbscit@yahoo.co.in
7428323290
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900