using System ;
using System.Data.OleDb ;
using System.Windows.Forms ;
// 導入程序中用的的所有名稱(chēng)空間
class OleDbTest {
public static void Main ( )
{
string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Application.StartupPath + "\\my.mdb" ;
OleDbConnection aConnection = new OleDbConnection ( strConnect ) ;
// 建立指向數據庫的連接
OleDbCommand aCommand = new OleDbCommand ( "select * from Persons" ,
aConnection ) ;
// 設計所需要返回的數據集的內容
try {
aConnection.Open ( ) ;
// 打開(kāi)指向數據庫連接
OleDbDataReader aReader = aCommand.ExecuteReader ( ) ;
// 返回需要的數據集內容
Console.WriteLine ( "以下就是打開(kāi)后的數據集的一個(gè)字段的所有內容!" ) ;
while ( aReader.Read ( ) ) {
Console.WriteLine ( aReader.GetString (0) ) ;
}
// 屏幕輸出數據集的第一個(gè)字段的所有內容,如果要第二個(gè)字段把"0"改為"1"
aReader.Close ( ) ;
// 關(guān)閉數據集
aConnection.Close ( ) ;
// 關(guān)閉指向數據庫的連接
}
catch ( OleDbException e )
{
Console.WriteLine ( "錯誤類(lèi)型:", e.Errors[0].Message ) ;
// 如果出錯,輸出錯誤信息
}
}
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。