连接并读取C#.MDB项目 [英] connect and read .MDB item with C#

查看:227
本文介绍了连接并读取C#.MDB项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以连接到本地MDB文件,并选择信息的单个位出来的吗?
我有一个表,在它的信息的一个位一个.mbd文件。我想有该记录被输出成一个参考禁用的文本框。我相信我能拿到DB开放,并运行查询,但不知道什么,我需要从中读取数据。



感谢

  VAR myDataTable =新的DataTable();使用(VAR连接如=新的OleDbConnection(提供者= Microsoft.JET.OLEDB.4.0;
+数据源= C:\\menus\\\\
ewmenus\\menu.mdb;密码= ****))
{
conection.Open();
VAR的查询=选择SITEID从n_user
变种适配器=新OleDbDataAdapter的(查询,连接如);
OleDbCommandBuilder oleDbCommandBuilder =新的OleDbCommandBuilder(适配器);
}


解决方案

如果只是要读取单场在你的数据库表,你可以使用一个 OleDbDataReader 在结果返回所需的字段。

  VAR myDataTable =新的DataTable(),可以循环和;使用(VAR连接如=新的OleDbConnection(提供者= Microsoft.JET.OLEDB.4.0;
+数据源= C:\\menus\\\\
ewmenus\\menu.mdb;密码= ****))
{
conection.Open();
VAR的查询=选择SITEID从n_user
VAR命令=新的OleDbCommand(查询,连接如);
VAR读卡器= Command.ExecuteReader却();
,而(reader.Read())
textBox1.Text =读卡器[0]的ToString();

}

如果你只有一个记录,只有一个字段,一个更好的解决方案是方法的ExecuteScalar

  conection.Open(); 
//返回才刚刚一个字段
VAR查询组成=选择SITEID从n_user其中userid = 1的一个记录的查询;
VAR命令=新的OleDbCommand(查询,连接如);
INT结果=(int)的command.ExecuteScalar(); //假设SITEID是一个整数



也许我还要提到的ExecuteScalar返回的如果查询没有找到的用户ID匹配,所以最好要小心这里的转换

 对象result = command.ExecuteScalar(); 
如果(结果!= NULL)
INT用户ID =(int)的结果;
.....


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);
        }

解决方案

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..

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

     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 null if the query doesn't find a match for the userid, so it is better to be careful with the conversion here

     object result = command.ExecuteScalar();
     if( result != null)
        int userID  = (int)result;
        .....

这篇关于连接并读取C#.MDB项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆