C#Odbc, Oledb로 Excel 및 CSV 조회

6716 단어 C# 기반

1. 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();
            }
        }

좋은 웹페이지 즐겨찾기