本人初学MFC,接触数据库编程也是这次课程设计的时候才遇到。现在在做一个简单的仓库物资管理系统,主要是有一个登陆界面,还有入库信息,出库信息,查询等功能。现在遇到一个问题就是,在我插入信息的时候,点击确定之后,程序没有出错,但是在数据库中并没有更新信息。数据的连接是没有问题的,因为在登陆的时候我调用了用户信息表中的数据作为登陆的依据,现在登陆已经没有问题了。我个人觉得是因为在插入数据的时候没有指明操作的表造成的,不过不知道怎么解决。还有就是,connectionptr,recordsetptr对象是不是用一次就要实例化一次?PS:第一次实例我是放在APP的initinstance里的,之后就没有在实例化,不知道这样有没有问题,因为几个对话框都要用到这两个对象,所以,我把它们设置成全局的,不知道有没有问题。下面贴出主要代码,请高手帮忙解决。不胜感激!!!!!(环境为VS2010)
CLoginApp theApp;
 _ConnectionPtr cnn;
 _RecordsetPtr rst;
 //_Command cmd;
// CLoginApp 初始化BOOL CLoginApp::InitInstance()
{
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControlsEx()。否则,将无法创建窗口。
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括所有要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance();
AfxEnableControlContainer();
CoInitialize(NULL);
cnn = NULL;
try
{
cnn.CreateInstance(_uuidof(Connection));
rst.CreateInstance(_uuidof(Recordset));

cnn->ConnectionString = "Provider=SQLOLEDB.1;Password=yuan514;Persist Security Info=True;User ID=yuan;Initial Catalog=StorageInfo";
cnn->Open("", "", "",adConnectUnspecified);
}
catch(_com_error &e)
{
CoUninitialize();
AfxMessageBox(e.ErrorMessage());
return FALSE;
}插入数据操作:void CInputdlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnOK();
//rst = cnn->Execute("select * from ImportTable", NULL, adCmdText);使用这句会出现下图错误
int n = MessageBox("确定添加新记录么?","系统提示", MB_OK | MB_ICONSTOP);
if(n == MB_OK)
{
UpdateData(TRUE); rst->MoveLast();
rst->AddNew();
rst->PutCollect("in_num", _variant_t(m_strInNum));
rst->PutCollect("in_name", _variant_t(m_strInName));
rst->PutCollect("in_producer", _variant_t(m_strProducer));
rst->PutCollect("in_type", _variant_t(m_strType));
rst->PutCollect("in_unit", _variant_t(m_strUnit));
rst->PutCollect("in_counts", _variant_t(m_longInCounts));
rst->PutCollect("in_date", _variant_t(m_strDate));
rst->PutCollect("in_company", _variant_t(m_strCompany));
rst->PutCollect("in_deliverman", _variant_t(m_strDeliver)); rst->Update();
}
}

解决方案 »

  1.   


    就是因为书上讲的不懂才来这里发帖的。我觉得是因为数据库中有多张表,我在操作的时候没有指明对象,所以才插入不了数据。但是,我用rst->execute()方法老是出错,不知道为什么,急死我了,今天在线守帖居然没人回第一次发帖就石沉大海了。