程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 程序員參考--OLE DB 教學文章

C# 程序員參考--OLE DB 教學文章

編輯:C#入門知識

OLE DB 是用於訪問數據的基於 COM 的應用程序編程接口 (API)。OLE DB 支持訪問以 OLE DB 提供程序可以使用的任何格式(數據庫、電子表格、文本文件等)存儲的數據。每個 OLE DB 提供程序從某一特定類型的數據源(例如 SQL Server 數據庫、Microsoft Access 數據庫或 Microsoft Excel 電子表格)公開數據。

本教程說明如何從 C# 應用程序中使用 Microsoft Access 數據庫。

教程

本教程說明如何從 C# 中使用 Microsoft Access 數據庫。它顯示如何創建數據集並從數據庫向該數據集添加表。本示例程序中使用的 BugTypes.mdb 數據庫是 Microsoft Access 2000 .MDB 文件。

示例

本程序訪問 BugTypes.mdb 數據庫,創建一個數據集並向其中添加表,然後顯示表、列和行的數目。它還顯示每行的標題。

// OleDbSample.cs



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

[1] [2] 下一頁  

: 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]); } } }

輸出

BugTypes.mdb 數據庫的 Categories 表包含下列信息:

類別 ID 類別名稱 1 Bugbash Stuff 2 Appweek Bugs 3 .NET Framework Reports 4 Internal Support

當運行此示例時,屏幕上將顯示下列輸出:

Found data table Categories



1 tables in data set



1 tables in data set



4 rows in Categories table



2 columns in Categories table



Column name[0] is CategoryID, of type System.Int32



Column name[1] is CategoryName, of type System.String



CategoryName[1] is Bugbash Stuff



CategoryName[2] is Appweek Bugs



CategoryName[3] is .NET Framework Reports



CategoryName[4] is Internal Support

 

上一頁  [1] [2] 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved