C#操作mdb数据库文件

C#操作mdb数据库文件


源码如下:

namespace CSharpWindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string _fileName;
        private string _connectionString;
        private OleDbConnection _odcConnection;

        private void button1_Click(object sender, EventArgs e)
        {
            //根据IMEI 862075035979565 进行查找
            string exefilepath = System.IO.Directory.GetCurrentDirectory();
            string mdbfilepath = exefilepath + "\\M02C_KMD_ST2017040801_13000.MDB";
            string mdbFile = mdbfilepath;
            string strimei = "7762075035979565";
            //MAC在数据库文件中的名称
            string strMACSection = "";
            //IMEI在数据库文件中的名称
            string strIMEISection = "";
            //SN在数据库文件中的名称
            string strSNSection = "";

            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                this._fileName = mdbFile;
                this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbFile + ";";
                //1、建立连接 C#操作Access之读取mdb 
                this._odcConnection = new OleDbConnection(this._connectionString);
                //2、打开连接 C#操作Access之读取mdb 
                this._odcConnection.Open();
                //建立SQL查询   
                OleDbCommand odCommand = _odcConnection.CreateCommand();
                //3、输入查询语句 C#操作Access之读取mdb  
                odCommand.CommandText = "select * from " + "SNTable";
                //建立读取   
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据   
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i++)
                {
                    DataColumn dc;
                    string sstr = odrReader.GetName(i);
                    sstr.ToLower();
                    if (0==sstr.IndexOf("MAC"))
                    {
                        strMACSection = odrReader.GetName(i);
                    }
                    else if (0 == sstr.IndexOf("IMEI"))
                    {
                        strIMEISection = odrReader.GetName(i);
                    }
                    else if (0 == sstr.IndexOf("SN"))
                    {
                        strSNSection = odrReader.GetName(i);
                    }
                    else
                    {

                    }

                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
                while (odrReader.Read())
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i++)
                    {
                        string s1 = odrReader.GetName(i);
                        string s2 = odrReader[s1].ToString();
                        string s3 = odrReader[s1].ToString();
                        if (-1 != s1.IndexOf(strIMEISection) && -1 != s3.IndexOf(strimei))
                        {
                            //string strMAC = "MAC";
                            string s4 = odrReader[strMACSection].ToString();
                            //00:1E:C8:98:9C:F1
                            //string strSN = "SN";
                            string s5 = odrReader[strSNSection].ToString();
                            //M02C17051501447 

                            //关闭连接 C#操作Access之读取mdb  
                            odrReader.Close();
                            _odcConnection.Close();

                            return;
                        }
                        dr[s1] = s3;
                        //dr[odrReader.GetName(i)] =
                        //odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接 C#操作Access之读取mdb  
                odrReader.Close();
                _odcConnection.Close();
                //success = true;
            }
            catch
            {
                //success = false;
            }            
        }
    }
}

C#操作mdb文件引用的库为

using System.Data;
using System.Data.OleDb;



  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法哥2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值