用CDatabase类的open方法打开一个新建的excel文件,打开正常,但是用execute方法执行create table时却得到异常,说“不能修改表XXX的设计,因为它在只读数据库中”,如何解决,程序段如下 CDatabase dbExcel;
CString strConnect,strFileName;
GetModuleFileName(NULL,strFileName.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
strFileName.ReleaseBuffer();
int nPos;
nPos=strFileName.ReverseFind('\\');
strFileName=strFileName.Left(nPos);
strFileName+="\\test.xls";
strConnect.Format("ODBC;DRIVER={MICROSOFT EXCEL DRIVER (*.XLS)};DSN=\'\';DBQ=%s",strFileName);
try
{
dbExcel.Open(NULL,false,false,strConnect);
}
catch (CDBException e1)
{
MessageBox("Fail to open the Excel file!\n"+e1.m_strError);
return;
}
MessageBox("Successfully open the Excel file!");// 以上没有问题 try
{
this->dbExcel.ExecuteSQL("CREATE TABLE TEST1(NUM INT)");
}
catch (CDBException e)
{
MessageBox("Fail to open the Excel file!\n");
return;
}
MessageBox("Successfully open the Excel file!");
// 抛出异常
CString strConnect,strFileName;
GetModuleFileName(NULL,strFileName.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
strFileName.ReleaseBuffer();
int nPos;
nPos=strFileName.ReverseFind('\\');
strFileName=strFileName.Left(nPos);
strFileName+="\\test.xls";
strConnect.Format("ODBC;DRIVER={MICROSOFT EXCEL DRIVER (*.XLS)};DSN=\'\';DBQ=%s",strFileName);
try
{
dbExcel.Open(NULL,false,false,strConnect);
}
catch (CDBException e1)
{
MessageBox("Fail to open the Excel file!\n"+e1.m_strError);
return;
}
MessageBox("Successfully open the Excel file!");// 以上没有问题 try
{
this->dbExcel.ExecuteSQL("CREATE TABLE TEST1(NUM INT)");
}
catch (CDBException e)
{
MessageBox("Fail to open the Excel file!\n");
return;
}
MessageBox("Successfully open the Excel file!");
// 抛出异常
sSql.Format("DRIVER={%s};DSN='''';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
sDriver, sExcelFile, sExcelFile); // 创建数据库 (既Excel表格文件)
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// 创建表结构(姓名、年龄)
sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);