以下是两种数据库访问的公用方法:

1、OleDb驱动:

    /// <summary>
    /// Oledb数据库驱动
    /// </summary>
    public class OledbHelper
    {
        #region ExcelHandler
        /// <summary>
        /// 获取OleDbConnection
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <returns></returns>
        private static OleDbConnection GetCon(string filePath)
        {
            string strCon = string.Empty;
            string extension = Path.GetExtension(filePath);
            if (extension == ".xls")
                strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;IMEX=1'";
            else
                strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties='Excel 12.0;HDR=YES'";
            return new OleDbConnection(strCon);
        }
        /// <summary>
        /// 获取Excel中全部的的sheet名称
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <returns></returns>
        public static DataTable GetSheets(string filePath)
        {
            DataTable dtSn = new DataTable();
            dtSn.Columns.Add("工作表", typeof(string));
            OleDbConnection con = GetCon(filePath);
            try
            {
                DataTable dtSheetName = new DataTable();
                string sheetName = string.Empty;
                con.Open();
                DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                foreach (DataRow row in dt.Rows)
                {
                    DataRow dr = dtSn.NewRow();
                    dr["工作表"] = row["TABLE_NAME"];
                    dtSn.Rows.Add(dr);
                }
            }
            catch (System.Exception ex)
            {
                con.Close();
                throw ex;
            }
            con.Close();
            return dtSn;

        }
        /// <summary>
        /// 获取excel中的数据DataTable
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <param name="sheet">工作表名称</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string filePath, string sheet)
        {
            OleDbConnection con = GetCon(filePath);
            con.Open();
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT * FROM [" + sheet + "]";
                System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(sql, con);
                myCommand.Fill(dt);
            }
            catch (System.Exception ex)
            {
                con.Close();
                throw;
            }
            con.Close();
            return dt;
        }
        /// <summary>
        /// 获取excel中的数据DataSet
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <param name="sheet">工作表名称</param>
        /// <returns></returns>
        public static DataSet GetDataSet(string filePath, string[] sheets)
        {
            OleDbConnection con = GetCon(filePath);
            con.Open();
            DataSet ds = new DataSet();
            try
            {
                foreach (string sheet in sheets)
                {
                    if (ds.Tables.Contains(sheet))
                        continue;
                    string sql = "SELECT * FROM [" + sheet + "]";
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, con);
                    myCommand.Fill(ds, sheet);
                }
            }
            catch (System.Exception ex)
            {
                con.Close();
                throw;
            }
            con.Close();
            return ds;
        }
        #endregion
    }
View Code

相关文章: