1)在Access數據庫 Database1.mdb 中有壹個表:表1
2)用OleDbDataReader 獲取指定行、指定列的值
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Data;
using?System.Data.OleDb;
namespace?ConsoleOleDb
{
class?Program
{
static?void?Main(string[]?args)
{
OleDbConnection?conn?=?
new?OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=|DataDirectory|\Database1.mdb");
OleDbCommand?cmd?=conn.CreateCommand();
cmd.CommandText?="select?*?from?表1";
conn.Open();
OleDbDataReader?rd?=?cmd.ExecuteReader();
//獲取表中第二行,第二列的值
string?s?=?(string)?GetValue(rd,?1,?2);
Console.WriteLine("表1第2行,第二列的值為:{0}",?s);
}
//?從OleDbDataReader中獲取指定行、指定列的值
static?object?GetValue(OleDbDataReader?reader,?int?row,?int?col)
{
int?i?=?0;
object?value?=?null;
while?(reader.Read())
{
if?(i?==?row)
{
value?=?reader[col];
break;
}
i++;
}
reader.Close();
return?value;
}
}
}