_CommandPtr m_pCommand;
_ConnectionPtr m_pConnection; // 初始化COM,创建ADO连接等操作
CoInitialize(NULL);
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cvd.mdb", "", "", adModeUnknown );
}
catch (_com_error &e)
{
cout<<e.ErrorMessage()<<endl;
}
// 使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM cvd", // 查询cvdfirst表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenStatic,
adLockOptimistic,
adCmdText);
}
catch(_com_error &e)
{
cout<<e.ErrorMessage()<<endl;
} m_pCommand = m_pConnection->Execute("alter table cvd drop column JOB", NULL, 0);
_ConnectionPtr m_pConnection; // 初始化COM,创建ADO连接等操作
CoInitialize(NULL);
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cvd.mdb", "", "", adModeUnknown );
}
catch (_com_error &e)
{
cout<<e.ErrorMessage()<<endl;
}
// 使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM cvd", // 查询cvdfirst表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenStatic,
adLockOptimistic,
adCmdText);
}
catch(_com_error &e)
{
cout<<e.ErrorMessage()<<endl;
} m_pCommand = m_pConnection->Execute("alter table cvd drop column JOB", NULL, 0);
用连接对象的执行方法时,不需要再用记录集对象打开表。1、先连接数据库,保证连接成功;
2、直接执行删除列的SQL语句:CString strSQL = "alter table cvd drop column JOB";try
{
m_pConnection->Execute((_bstr_t)strSQL,NULL,adCmdText);
}
catch(_com_error &e)
{
cout < <e.Description() < <endl;
}