// 테이블 생성 및 데이터 삽입
#include <afxdb.h>
#include <atlstr.h>
#include <stdio.h>
int main()
{
CDatabase m_db;
CString sDriver = L"MICROSOFT EXCEL DRIVER (*.XLS)";
CString sExcelFile = L"test.XLS";
CString sSql;
if(m_db.IsOpen() == TRUE)
{
m_db.Close();
}
//테이블 생성
sSql.Format(L"DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
sDriver, sExcelFile, sExcelFile);
if(m_db.OpenEx(sSql, CDatabase::noOdbcDialog))
{
sSql.Format(L"CREATE TABLE %s (%s, %s, %s)"
,_T("TEST3")
,_T("Seq NUMBER")
,_T("Name TEXT")
,_T("Phone TEXT")
);
m_db.ExecuteSQL(sSql);
}
//레코드 삽입
sSql.Format(L"INSERT INTO %s(Seq, Name, Phone) VALUES(%d, '%s', '%s')"
,_T("TEST3")
,1
,_T("홍길동")
,_T("123-4567")
);
m_db.ExecuteSQL(sSql);
if(m_db.IsOpen() == TRUE)
{
m_db.Close();
}
}
// 테이블 읽어오기
#include <afxdb.h>
#include <atlstr.h>
#include <stdio.h>
int main()
{
CDatabase m_db;
CString sDriver = L"MICROSOFT EXCEL DRIVER (*.XLS)";
CString sExcelFile = L"test.XLS";
CString sSql;
if(m_db.IsOpen() == TRUE)
{
m_db.Close();
}
sSql.Format(L"DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
sDriver, sExcelFile, sExcelFile);
if(m_db.OpenEx(sSql, CDatabase::noOdbcDialog))
{
sSql.Format(L"SELECT * FROM %s", L"TEST3");
CRecordset rs(&m_db);
rs.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
CString sFields[20];
int iFieldCount = 0;
iFieldCount = rs.GetODBCFieldCount();
wprintf(L"%d\n", iFieldCount);
while(!rs.IsEOF())
{
for(int i=0; i< iFieldCount; i++)
{
//한글을 못읽어오는 문제점이 있음
rs.GetFieldValue((short) i, sFields[i]);
wprintf(L"%s\n", sFields[i]);
}
rs.MoveNext();
}
}
if(m_db.IsOpen() == TRUE)
{
m_db.Close();
}
}
'Development > C/C++' 카테고리의 다른 글
Console Function - MSDN (0) | 2011.08.13 |
---|---|
Chinese Character Codes (0) | 2011.08.13 |
cast 연산자 (0) | 2011.08.13 |
c++ 추상 기초클래스 (0) | 2011.08.13 |
Byte Order (0) | 2011.08.13 |