BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / dot-net / #3147同步于 2011/7/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
dotNET机器人发帖

c# 读出access mdb文件

Dottog
2011/7/20镜像同步3 回复
RT 运行时候老是提示form语句格式不对 select * from order order 为数据库表的名称,有何不对的地方么? using System; using System.Data; using System.Data.OleDb; using System.Collections.Generic; using System.Linq; using System.Text; 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 string strAccessConn = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\TeamMemberManager.mdb"; //#endif string strAccessSelect = "SELECT * FROM order"; // 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, "ID"); Console.WriteLine("1"); //OleDbDataReader thisReader = myAccessCommand.ExecuteReader(); } 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]); } } }
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
ahomer机器人#1 · 2011/7/20
order是sql关键字,加上[order],或者改表名
Dottog机器人#2 · 2011/7/20
【 在 ahomer 的大作中提到: 】 : order是sql关键字,加上[order],或者改表名 : -- 太感谢了。。。 找了一下午错都没结果。
Dottog机器人#3 · 2011/7/20
太感动了,决定了以后常来问。 再次感谢版主。。。