在c#和OLEDB中搜索数据表 [英] Searching Datatable in c# and OLEDB
本文介绍了在c#和OLEDB中搜索数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何在c#和OLEDB中搜索mdb文件的数据表。
我只是一个初学者,我需要一步一步的帮助。
谢谢。
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.
推荐答案
你应该尝试使用LINQ,如果你知道SQL它的快速和简单学习
http://msdn.microsoft.com/en-us /library/bb548891.aspx [ ^ ]
http://technet.microsoft.com/en-us/subscriptions/bb397900(v = vs.90).aspx [ ^ ]
You should try using LINQ, its quick and easy to learn if you know SQL
http://msdn.microsoft.com/en-us/library/bb548891.aspx[^]
http://technet.microsoft.com/en-us/subscriptions/bb397900(v=vs.90).aspx[^]
Hi User / Gust,
请解释确切的要求。我对你的要求感到困惑。所以一般来说我建议多个例子或不同的方式你可以实现你的要求。
首先
-----
这个程序访问BugTypes .mdb数据库,创建数据集,向其添加表,并显示表,列和行的数量。它还显示每行的标题。
--------------------------------- -------------------------------------------------
使用System;
使用System.Data;
使用System.Data.OleDb;
使用System.Xml .Serialization;
公共类MainClass {
public static void Main()
{
//设置Access连接并选择字符串。
//如果你构建
//来自命令的示例,必须更改BugTypes.MDB的路径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;
//创建数据集并向其添加Categories表:
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
尝试
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine(错误:无法创建数据库连接。 \ n {0},ex.Message);
返回;
}
尝试< br $>
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand) );
myAccessConn.Open();
myDataAdapter.Fill(myDataSet,Categories);
}
catch(例外情况)
{
Console.WriteLine(错误:无法检索来自DataBase.\ {0},ex.Message的所需数据;
返回;
}
终于
{
myAccessConn.Close();
}
// A数据集可以包含多个表,所以让它们将它们全部放入数组中:
DataTableCollection dta = myDataSet.Tables;
foreach(数据表dt in dta)
{
Console.WriteLine(找到数据表{0},dt.TableName);
}
//接下来的两行显示了两种不同的方法,你可以获得数据集中的表数
//: br />
Console.WriteLine(数据集中的{0}表,myDataSet.Tables.Count);
Console.WriteLine(数据集中的{0}表, dta.Count);
//接下来的几行显示如何从数据集中按名称获取有关
//特定表的信息:
Console.WriteLine(类别表中的{0}行,myDataSet.Tables [Categories]。Rows.Count);
//列信息是自动从数据库中提取,
//所以我们可以在这里阅读:
Console.WriteLine(类别表中的{0}列,myDataSet.Tables [类别] .Columns.Count);
DataColumnCollection drc = myDataSet.Tables [Categories]。列;
int i = 0;
foreach(drc中的DataColumn dc)
{
//打印列下标,然后是列的名称
//及其数据类型:
Console.WriteLine(列名[{0}]为{1},类型为{2},i ++,dc.ColumnName,dc.DataType);
}
DataRowCollection dra = myDataSet.Tables [Categories]。行;
foreach(DataRow dr in dra)
{
//打印CategoryID作为下标,然后打印CategoryName:
Console.WriteLine (CategoryName [{0}]为{1},dr [0],dr [1]);
}
}
}
-------------------------------- --------------------------------------------------
输出
BugTypes.mdb数据库的Categories表包含以下信息。
类别ID类别名称
----------- -------------
1 Rahul Kumar
2 Abhishek Jaishwal
3 .NET Framework 4.5
4 Windows 8 Super
---- --------------------------------
Second
- ------
此示例使用OleDbDataAdapter使用ADO Recordset填充DataTable。此示例假定您已创建ADO记录集
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);
//使用ADO库中的ADO对象(msado15.dll)使用TlbImp.exe导入
//作为.NET库ADODB.dll
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();
-------------------------------------------- ----------------------------------
还有一些有用的链接: 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 [ ^ ]
-------------------------------------------------- -----------------
进一步你可以谈谈更多细节。
Rahul Kumar,
rahulinbscit@yahoo.co.in
7428323290
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
MSDN OldDb教程 [ ^ ]
和另一个 connect-to-microsoft-access-mdb-database -using-csharp / [ ^ ]和此处更多 [ ^ ]
MSDN OldDb Tutorial[^]
and another one connect-to-microsoft-access-mdb-database-using-csharp/[^] and even more here[^]
这篇关于在c#和OLEDB中搜索数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文