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();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Odbc, Oledb로 Excel 및 CSV 조회1. Odbc, Oledb로 Excel 및 CSV 조회 1. Odbc로 CSV 조회 2, Oledb로 CSV 조회 3, Odbc로 Excel 조회 4, Oledbc로 Excle 조회...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.