查询Access弹出“IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败!”错误,如何解决

查询Access弹出“IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败!”错误,如何解决

日常中,用Access来写程序,可以免除大型数据库的安装,程序可以直接运行。

单发现写的SQL语句,在ACCESS里查询都没问题的情况下,在程序中经常出错,原来是 触动了ACCESS关键字的问题。

1.写一个combobox的选择值改变的触发事件,上码 举例:

        private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
        {
            #region textbox8 获取名称
            string str21 = comboBox3.Text.ToString();//制造商型号
            string str22 = " where [id] = '" + str21 + "'";
            string str23 = "[id],[names],[phone],[address],[contacts],[remarks]";
            ManuFactInfo matInfo1 = dal1.getManu(str23, str22);
            //textBox4.Text = @"select " + str23 + " from ManuFactInfo" + str22;
            textBox8.Text = matInfo1.names;//制造商名称

            #endregion
        }

2.对应的DAL类的getManu方法,获取实体类ManuFactInfo

public ManuFactInfo getManu(string field, string wherecom)
        {
            ManuFactInfo manufo1 = new ManuFactInfo();
            if (!File.Exists(accessPath))//检查文件是否存在
            {
                MessageBox.Show("Access文件不存在根目录", "警告");
            }
            OleDbConnection dbcon = new OleDbConnection(conStr);
            dbcon.Open();
            // string sqlcomd = "select " + field + " from " + tablename ;
            string sqlcomd = @"select " + field + " from ManuFactInfo" + wherecom;
            //string sqlcomd = @"select [id],[names],[phone],[address],[contacts],[remarks] from ManuFactInfo where [id] = 'TPE2412-50000-8-Y'";
            OleDbCommand dbcom = new OleDbCommand(sqlcomd, dbcon);
            OleDbDataReader dr = dbcom.ExecuteReader();
            if (dr.Read())
            {
                manufo1.id = dr["id"].ToString();
                manufo1.names = dr["names"].ToString();
                manufo1.phone = dr["phone"].ToString();
                manufo1.address = dr["address"].ToString();
                manufo1.contacts = dr["contacts"].ToString();
                manufo1.remarks = dr["remarks"].ToString();
                dbcon.Close();
                dr.Close();
                return manufo1;
            }
            else
            {
                dbcon.Close();
                dr.Close();
                return manufo1;
            }
        }

3.实体类ManuFactInfo

  class ManuFactInfo
    {
        public string id { get; set; }//制造商型号
        public string names { get; set; }//制造商名称
        public string phone { get; set; }//联系电话
        public string address { get; set; }//地址
        public string contacts { get; set; }//联系人
        public string remarks { get; set; }//备注
    }

最后:

此 项目纯属练手,后续会把所有代码奉上。

因为项目在实际应用中,不过对我个人而言,没有利益,以及水平有限,代码写的乱哄哄,凑合着用。

一句话,赶时间。哈哈。

猜你喜欢

转载自www.cnblogs.com/Xinqing2010/p/12579372.html