C#用Odbc、Oledb查询Excel和CSV

一、用Odbc、Oledb查询Excel和CSV

1、用Odbc查询CSV
        /// 
        /// Odbc查询CSV
        /// 
        public static void QueryCSVToOdbc()
        {
            //文件路径
            string tableName = "file.csv";
            string filePath = AppDomain.CurrentDomain.BaseDirectory;

            string pContent = string.Empty;

            OdbcConnection odbcConn = new OdbcConnection();
            OdbcCommand odbcCmd = new OdbcCommand();
            OdbcDataReader dataReader;
            try
            {            
                string strConnOledb = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=";
                strConnOledb += filePath;
                strConnOledb += ";Extensions=csv,txt;";

                odbcConn.ConnectionString = strConnOledb;
                odbcConn.Open();
                StringBuilder commandText = new StringBuilder("SELECT ");
                commandText.AppendFormat("* From {0}", tableName);
                odbcCmd.Connection = odbcConn;
                odbcCmd.CommandText = commandText.ToString();
                dataReader = odbcCmd.ExecuteReader();


                while (dataReader.Read())
                {
                    pContent = Convert.ToString(dataReader["content"]);
                }
                dataReader.Close();
            }
            catch (System.Exception ex)
            {
                odbcConn.Close();
            }
            finally
            {
                odbcConn.Close();
            }
        }
2、用Oledb查询CSV
        /// 
        /// Oledb查询CSV
        /// 
        public static void QueryCSVToOledb()
        {
            //文件路径
            string tableName = "file.csv";
            string filePath = AppDomain.CurrentDomain.BaseDirectory;

            string pContent = string.Empty;

            OleDbConnection oledbConn = new OleDbConnection();
            OleDbCommand oledbCmd = new OleDbCommand();
            OleDbDataReader dataReader;
            try
            {
                //两种连接方式皆可
                //string strConnOledb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
                string strConnOledb="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
                strConnOledb += filePath;
                strConnOledb += ";Extended Properties='Text;HDR=Yes;IMEX=1;'";

                oledbConn.ConnectionString = strConnOledb;
                oledbConn.Open();
                StringBuilder commandText = new StringBuilder("SELECT ");
                commandText.AppendFormat("* From {0}", tableName);
                oledbCmd.Connection = oledbConn;
                oledbCmd.CommandText = commandText.ToString();
                dataReader = oledbCmd.ExecuteReader();

                
                while (dataReader.Read())
                {
                    pContent = Convert.ToString(dataReader["content"]);
                }
                dataReader.Close();
            }
            catch (System.Exception ex)
            {
                oledbConn.Close();
            }
            finally
            {
                oledbConn.Close();
            }
        }
3、用Odbc查询Excel
        /// 
        /// Odbc查询Excel
        /// 
        public static void QueryExcelToOdbc()
        {
            //文件路径
            string tableName = "file.xls";
            string filePath = AppDomain.CurrentDomain.BaseDirectory + tableName;

            OdbcConnection odbcConn = new OdbcConnection();
            OdbcCommand odbcCmd = new OdbcCommand();
            OdbcDataReader dataReader;
            try
            {
                //连接字符串
                string strConnOledb = "Driver={Microsoft Excel Driver (*.xls)};Dbq=";
                strConnOledb += filePath;
                strConnOledb += ";Extended=xls";

                odbcConn.ConnectionString = strConnOledb;
                odbcConn.Open();
                StringBuilder commandText = new StringBuilder("SELECT ");
                commandText.AppendFormat("* From {0}", "[Sheet1$]");
                odbcCmd.Connection = odbcConn;
                odbcCmd.CommandText = commandText.ToString();
                dataReader = odbcCmd.ExecuteReader();
                while (dataReader.Read())
                {
                    string pContent = Convert.ToString(dataReader["content"]);
                }
                dataReader.Close();
            }
            catch (System.Exception ex)
            {
                odbcConn.Close();
            }
            finally
            {
                odbcConn.Close();
            }
        }
4、用Oledbc查询Excle
        /// 
        /// Oledb查询Excel
        /// 
        public static void QueryExcelToOledb()
        {
            //文件路径
            string tableName = "file.xls";
            string filePath = AppDomain.CurrentDomain.BaseDirectory + tableName;

            OleDbConnection oledbConn = new OleDbConnection();
            OleDbCommand oledbCmd = new OleDbCommand();
            OleDbDataReader dataReader;
            try
            {
                //一下两种方式皆可
                //string strConnOledb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
                //strConnOledb += filePath;
                //strConnOledb += ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";

                string strConnOledb = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
                strConnOledb += filePath;
                strConnOledb += ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";

                oledbConn.ConnectionString = strConnOledb;
                oledbConn.Open();
                StringBuilder commandText = new StringBuilder("SELECT ");
                commandText.AppendFormat("* From {0}", "[Sheet1$]");
                oledbCmd.Connection = oledbConn;
                oledbCmd.CommandText = commandText.ToString();
                dataReader = oledbCmd.ExecuteReader();
                while (dataReader.Read())
                {
                    string pContent = Convert.ToString(dataReader["content"]);
                }
                dataReader.Close();
            }
            catch (System.Exception ex)
            {
                oledbConn.Close();
            }
            finally
            {
                oledbConn.Close();
            }
        }

你可能感兴趣的:(C#基础)