使用 C# 连接和读取 .MDB 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16906535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
connect and read .MDB item with C#
提问by ShaneTheTech
Is it possible to connect to a local MDB file and pick a single bit of info out of it ? I have a table in a .mbd file with a single bit of info in it. I would like to have that record be output into a disabled textbox for a reference. I believe I can get the DB open, and run the query but no idea what I need to read from it.
是否可以连接到本地 MDB 文件并从中选取一点信息?我在 .mbd 文件中有一个表,其中有一点信息。我希望将该记录输出到禁用的文本框中以供参考。我相信我可以打开数据库并运行查询,但不知道我需要从中读取什么。
thanks
谢谢
var myDataTable = new DataTable();
using (var conection = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=C:\menus\newmenus\menu.mdb;Password=****"))
{
conection.Open();
var query = "Select siteid From n_user";
var adapter = new OleDbDataAdapter(query, conection);
OleDbCommandBuilder oleDbCommandBuilder = new OleDbCommandBuilder(adapter);
}
采纳答案by Steve
To simply read a single field on your database table you could use an OleDbDataReaderthat could loop over the result and return the field required..
要简单地读取数据库表上的单个字段,您可以使用OleDbDataReader可以循环结果并返回所需的字段。
var myDataTable = new DataTable();
using (var conection = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=C:\menus\newmenus\menu.mdb;Password=****"))
{
conection.Open();
var query = "Select siteid From n_user";
var command = new OleDbCommand(query, conection);
var reader = command.ExecuteReader();
while(reader.Read())
textBox1.Text = reader[0].ToString();
}
if you have just one record and just one field then a better solution is the method ExecuteScalar
如果您只有一个记录和一个字段,那么更好的解决方案是方法ExecuteScalar
conection.Open();
// A query that returns just one record composed of just one field
var query = "Select siteid From n_user where userid=1";
var command = new OleDbCommand(query, conection);
int result = (int)command.ExecuteScalar(); // Supposing that siteid is an integer
Probably I should also mention that ExecuteScalar returns nullif the query doesn't find a match for the userid, so it is better to be careful with the conversion here
可能我还应该提到,如果查询没有找到用户 ID 的匹配项,则ExecuteScalar 返回null,所以最好小心这里的转换
object result = command.ExecuteScalar();
if( result != null)
int userID = (int)result;
.....
回答by aef
Are you looking for stm like this?
你在找这样的stm吗?
OleDbCommand cmd = new OleDbCommand();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
// read ur stuff here.
}
回答by Evan L
Yes very possible. Just have the adapter fill the DataTable, also I don't think you'll need the OleDbCommandBuilder.
是的,很有可能。只要有适配器填充的DataTable,也是我不认为你需要的OleDbCommandBuilder。
using (var conection = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=C:\menus\newmenus\menu.mdb;Password=****"))
{
conection.Open();
var query = "Select siteid From n_user";
var adapter = new OleDbDataAdapter(query, conection);
adapter.Fill(myDataTable);
myTextBox.Text = myDataTable.Rows[0][0].ToString();
}
Also I think using ExecuteScalarwould be a better solution, but my answer was tailored to the objects you had already instantiated.
此外,我认为使用ExecuteScalar会是一个更好的解决方案,但我的答案是针对您已经实例化的对象量身定制的。
回答by Andy G
You could use OleDbCommand.ExecuteScalarto retrieve a single value. It is returned as an object and you could cast it to the correct type.
您可以使用OleDbCommand.ExecuteScalar来检索单个值。它作为对象返回,您可以将其转换为正确的类型。

