编写一个访问数据库的简单程序:如下:
SDI模式
StdAfx.h中包含 #import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
CMainFrame里声明 _ConnectionPtr m_pConnection;
然后建立一个菜单,加入响应函数,内容如下
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
// m_pConnection->ConnectionString ="shop.mdb=e.udl"; // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
// 打开本地Access库shop.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=shop.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库shop.mdb是否在当前路径下!"); }
CCLUBINF AddClubDlg;
if(AddClubDlg.DoModal()==IDOK)
{
// srand( (unsigned)time( NULL ) );
MessageBox("即将保存数据");
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
m_pRecordset->Open("SELECT * FROM Club", // 查询Club表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Club_Name","丁丁");
m_pRecordset->PutCollect("Club_Num", _variant_t("413491374891"));
m_pRecordset->PutCollect("Club_Sex", false);
m_pRecordset->Update();
AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、AddClubDlg是我定义的一个用来让用户填信息的窗体,程序执行到MessageBox("即将保存数据");后,出现
Obnormal program termination错误而退出。。我以为是环境问题,就另建立一个窗口工程,用一个按钮来响应同样的事件,结果确依旧如此提示Runtime error,高手救救我!!!!
SDI模式
StdAfx.h中包含 #import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
CMainFrame里声明 _ConnectionPtr m_pConnection;
然后建立一个菜单,加入响应函数,内容如下
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
// m_pConnection->ConnectionString ="shop.mdb=e.udl"; // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
// 打开本地Access库shop.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=shop.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库shop.mdb是否在当前路径下!"); }
CCLUBINF AddClubDlg;
if(AddClubDlg.DoModal()==IDOK)
{
// srand( (unsigned)time( NULL ) );
MessageBox("即将保存数据");
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
m_pRecordset->Open("SELECT * FROM Club", // 查询Club表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Club_Name","丁丁");
m_pRecordset->PutCollect("Club_Num", _variant_t("413491374891"));
m_pRecordset->PutCollect("Club_Sex", false);
m_pRecordset->Update();
AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、AddClubDlg是我定义的一个用来让用户填信息的窗体,程序执行到MessageBox("即将保存数据");后,出现
Obnormal program termination错误而退出。。我以为是环境问题,就另建立一个窗口工程,用一个按钮来响应同样的事件,结果确依旧如此提示Runtime error,高手救救我!!!!
解决方案 »
- 同一个项目工程里,不同对话框里的控件资源ID号名字相同会冲突吗?vc2008
- 求VC这两本书的源代码 ??
- 请帮忙看看我这个多线程的程序使用临界区是不是出错了?
- 申请HTML/XML版斑竹
- CTreeCtrl中可以显示的条目文本的最大长度是多少?
- 请问各位大侠一个在CListView的排序的问题
- 请问如何在ATL建立的COM中调用DLL?问题解决另有高分相送
- 谁有winrar的压缩解压缩算法说明?在线等待!!!
- 获得本机Gateway地址和子网掩码
- 用win2000联对等网时出现的问题
- 使用浏览器控件Navigate2打开网页时的一些疑问?
- activex控件导入MFC静态库一变量时注册失败,请教原因。
CString sql="插入语句";
m_pConnection->Execute((_bstr_t)sql,NULL,adCmdText);试试
也就是说一创建那个m_pRecordset就直接出错了。。
这一句放到try里面。你估计拷贝的例子代码改的。问题不少。
HRESULT _hr = raw_Open(Source, ActiveConnection, CursorType, LockType, Options);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _hr;
}
单步跟进,提示在return _hr;这里发生内存异常
2.暂时不用窗口程序,在控制台程序中调试下代码,通过了再运用到窗体中。
3.不要使用动态游标,使用只读向前的游标和锁定类型,确认open成功了,在更换油表类型。
4.游标位置要设置为客户端游标
m_pConnection->CursorLocation = adUseClient;
void SQLAccess()
{
CoInitialize(NULL);
HRESULT hr = S_OK; // Define connection string variables.
_bstr_t strCnn("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Administrator\\Desktop\\test.mdb;Persist Security Info=False"); // Define ADO object pointers.
// Initialize pointers on define.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRst = NULL;
try
{
// Open connection.
pConnection.CreateInstance(__uuidof(Connection));
pConnection->Open (strCnn, "", "", adConnectUnspecified); // Please note that question , a question represents a parameter
_bstr_t strSQL("select * from table1;"); // Create command object.
pRst.CreateInstance(__uuidof(Recordset)); pRst->CursorLocation = adUseClient;
pRst->Open((_variant_t)strSQL, // 查询Club表中所有字段
pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText); pRst->AddNew();
pRst->PutCollect(_variant_t("id"),_variant_t("test0001"));
pRst->PutCollect(_variant_t("employee_name"),_variant_t("name1"));
pRst->Update(); // Clean up objects before exit.
if (pRst)
{
if (pRst->State == adStateOpen)
pRst->Close();
pRst.Release();
} if (pConnection)
{
if (pConnection->State == adStateOpen)
pConnection->Close();
pConnection.Release();
}
}
catch (_com_error &e)
{
// Print Com errors.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE(_T("Error\n"));
TRACE(_T("\tCode = %08lx\n"), e.Error());
TRACE(_T("\tErrorMessage = %s\n"), (LPCTSTR)e.ErrorMessage());
TRACE(_T("\tSource = %s\n"), (LPCTSTR)bstrSource);
TRACE(_T("\tDescription = %s\n"), (LPCTSTR)bstrDescription);
}
::CoUninitialize();
}这是一段调试通过的,你可以参考一下