当前位置:编程学习 > C#/ASP.NET >>

连接 Access 数据库,总是报错,怎么回事?

private void GetExcelData(string str)
        {
            try
            {
                OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
                connectStringBuilder.DataSource = str;
                connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";

                using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
                {
                    DataSet ds = new DataSet();
                    OleDbCommand cmdLiming = new OleDbCommand("SELECT * FROM [sheet1$]");
                    cn.Open();

                    using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
                    {
                        ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] { "tempDT" });
                        this.bindingSource1.DataSource = ds.Tables["tempDT"];
                        this.dataGridView1.DataSource = this.bindingSource1;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

错误代码:
System.Data.OleDb.OleDbException: 不可识别的数据库格式 'E:\qq.xls'。

   在 System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)

   在 System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)

   在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)

   在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)

   在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)

   在 System.Data.OleDb.OleDbConnection.Open()

   在 ReportERP.Form1.GetExcelData(String str) 位置 E:\ReportERP\ReportERP\Form1.cs:行号 51 --------------------编程问答-------------------- 它改成mdb格式 --------------------编程问答-------------------- 直接读Excel 也是这样的啊  --------------------编程问答--------------------
        public static DataSet ImportExcel(string file)
        {
            FileInfo fileInfo = new FileInfo(file);
            if (!fileInfo.Exists)
                return null;

            string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
            OleDbConnection objConn = new OleDbConnection(strConn);
            DataSet dsExcel = new DataSet();
            try
            {
                objConn.Open();
                //  string strSql = "select [date],[link],[linkformat],[addlink] from  [Sheet1$]";
                string strSql = "select * from  [Sheet1$]";
                OleDbDataAdapter odbcExcelDataAdapter = new OleDbDataAdapter(strSql, objConn);
                odbcExcelDataAdapter.Fill(dsExcel);
                return dsExcel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,