连接并读取.mdb项目与C#
是否可以连接到本地MDB文件并从中挑选一些信息? 我在.mbd文件中有一个表格,其中包含一个信息。我想将该记录输出到禁用的文本框中以供参考。我相信我可以打开DB,并运行查询,但不知道我需要从中阅读什么。
谢谢
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);
}
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.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要简单地读取数据库表上的一个字段,您可以使用 oledbdataReader 可能会循环到结果并返回所需的字段。
如果您只有一个记录,只有一个字段一个更好的解决方案是方法
可能我还应该提到executesCalar返回 null 如果查询找不到匹配用户ID,因此最好在此处谨慎转换
To simply read a single field on your database table you could use an OleDbDataReader that could loop over the result and return the field required..
if you have just one record and just one field then a better solution is the method ExecuteScalar
Probably I should also mention that ExecuteScalar returns null if the query doesn't find a match for the userid, so it is better to be careful with the conversion here
是的。只需让适配器填充
DataTable
,我也不认为您需要OLEDBCommandBuilder
。另外,我认为使用
executesCalar
是一个更好的解决方案,但是我的答案是针对您已经实例化的对象量身定制的。Yes very possible. Just have the adapter fill the
DataTable
, also I don't think you'll need theOleDbCommandBuilder
.Also I think using
ExecuteScalar
would be a better solution, but my answer was tailored to the objects you had already instantiated.您可以使用
You could use OleDbCommand.ExecuteScalar to retrieve a single value. It is returned as an object and you could cast it to the correct type.
您在寻找这样的STM吗?
Are you looking for stm like this?