當前位置:編程學習大全網 - 熱門推薦 - C#OleDbDataReader在命令提示符下怎麽獲取某行某列的值,能給句代碼嗎?

C#OleDbDataReader在命令提示符下怎麽獲取某行某列的值,能給句代碼嗎?

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;

}

}

}

  • 上一篇:“譜壹曲梁山伯與祝英臺”是那首歌的歌詞?
  • 下一篇:有哪些便宜好看的家居用品?
  • copyright 2024編程學習大全網