困扰太久了,发了几次贴,程序也下了一些,但好像还是没开窍,仍不会。程序是以ado访问sqlserver,将输入的数据插入数据库中,并显示在CListctrl中。1.程序初始化时,提出数据库中数据,并显示在CListctrl中。有一段代码,原形为: CString strTemp;
strTemp = "SELECT * FROM telelist WHERE name like '%" + m_strSelect + "%'";
m_ListCtrl.DeleteAllItems();
CADORecordset* pRs = new CADORecordset(CTeleApp::g_pDb);
CString TempSql;
int i = -1;
if(pRs->Open((LPCTSTR) strTemp ))
{
while(!pRs->IsEof())
{
LV_ITEM lvitem;
lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvitem.state = 0;
lvitem.stateMask = 0;
char temp[50];
lvitem.iItem = ++i;
lvitem.iSubItem = 0;
lvitem.pszText =itoa(i, temp, 10);
m_ListCtrl.InsertItem(&lvitem); pRs->GetFieldValue("name",TempSql);
m_ListCtrl.SetItemText(i, 1, (LPCTSTR)TempSql); pRs->GetFieldValue("tele",TempSql);
m_ListCtrl.SetItemText(i, 2, (LPCTSTR)TempSql); pRs->GetFieldValue("res",TempSql);
m_ListCtrl.SetItemText(i, 3, (LPCTSTR)TempSql); pRs->GetFieldValue("ID",TempSql);
m_ListCtrl.SetItemText(i, 4, (LPCTSTR)TempSql);
pRs->MoveNext();
}
pRs->Close();
}
else
AfxMessageBox("记录集创建失败");
delete pRs; 此处,pRs是什么类型指针?是不是等同
_RecordsetPtr m_pRecordset;
中的m_pRecordset?我用这个代替好像不行。
请指教一下这个能否改为m_pRecordset这种的?应该怎么用?
2.将用户输入的数据插入数据库的问题m_pConnection->Execute("INSERT INTO 表名(字段名1,字段名2,字段名3,字段名4,...) VALUES ('内容1', '内容2','内容3','内容4',...)",&ra,adCmdText);//往表里面添加数据在内容1、内容2等处只能写入具体的内容,不能用string来代替,有什么办法写字符串吗?还有别的方法,但我感觉这种好像更容易理解,请高手帮忙解答一下。万一不行用别的方法也行,谢谢!
strTemp = "SELECT * FROM telelist WHERE name like '%" + m_strSelect + "%'";
m_ListCtrl.DeleteAllItems();
CADORecordset* pRs = new CADORecordset(CTeleApp::g_pDb);
CString TempSql;
int i = -1;
if(pRs->Open((LPCTSTR) strTemp ))
{
while(!pRs->IsEof())
{
LV_ITEM lvitem;
lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvitem.state = 0;
lvitem.stateMask = 0;
char temp[50];
lvitem.iItem = ++i;
lvitem.iSubItem = 0;
lvitem.pszText =itoa(i, temp, 10);
m_ListCtrl.InsertItem(&lvitem); pRs->GetFieldValue("name",TempSql);
m_ListCtrl.SetItemText(i, 1, (LPCTSTR)TempSql); pRs->GetFieldValue("tele",TempSql);
m_ListCtrl.SetItemText(i, 2, (LPCTSTR)TempSql); pRs->GetFieldValue("res",TempSql);
m_ListCtrl.SetItemText(i, 3, (LPCTSTR)TempSql); pRs->GetFieldValue("ID",TempSql);
m_ListCtrl.SetItemText(i, 4, (LPCTSTR)TempSql);
pRs->MoveNext();
}
pRs->Close();
}
else
AfxMessageBox("记录集创建失败");
delete pRs; 此处,pRs是什么类型指针?是不是等同
_RecordsetPtr m_pRecordset;
中的m_pRecordset?我用这个代替好像不行。
请指教一下这个能否改为m_pRecordset这种的?应该怎么用?
2.将用户输入的数据插入数据库的问题m_pConnection->Execute("INSERT INTO 表名(字段名1,字段名2,字段名3,字段名4,...) VALUES ('内容1', '内容2','内容3','内容4',...)",&ra,adCmdText);//往表里面添加数据在内容1、内容2等处只能写入具体的内容,不能用string来代替,有什么办法写字符串吗?还有别的方法,但我感觉这种好像更容易理解,请高手帮忙解答一下。万一不行用别的方法也行,谢谢!
解决方案 »
- 大家有没有遇到过AfxMessageBox造成死机?
- 有少量玩家(大约三十分之一的玩家)能连5000端口,但是连不了2000端口
- 寒冬来临,散分取暖!
- 太多参数,期待为1,详见代码
- 请问CRecordset中如何添加新纪录呢?
- 请教如何实现一个基于对话框的报表打印,以及预览?高手教我,谢谢!
- SHGetFolderPath在那里声明的?
- 既然回调函数一般都写成全局函数,那我应该这样在全局函数里面往一个对话框类的编辑框里面写内容呢?
- 长作业问题!
- 大家可以给我讲讲 类与对象 概念好吗
- ★★★ 如何把Matlab的代码转化为C代码,在VC中运行? 内附Matlab代码(高分求解 300) ★★★
- AcceptEx投递后,完成前,如何得到连接对方的IP?
pRecord->AddNew(); pRecord->Fields->GetItem("name")->Value=_bstr_t(name);
pRecord->Fields->GetItem("sex")->Value=_bstr_t(sex);
pRecord->Fields->GetItem("age")->Value=short(age);
pRecord->Update();
2、
CString strAction;
CString strCon1 = "内容1";
CString strCon2 = "内容2";
...
strAction.Format("INSERT INTO 表名(字段名1,字段名2,字段名3,字段名4,...) VALUES ('%s', '%s',...)",strCon1,strCon2,...);
m_pConnection->Execute(strAction , &ra,adCmdText);
回abuseyoudna1981() :运行时出现错误,“ Runtime Error! ” “ abnormal program termination! ”,不知原因。
回double915(天涯沦落人):仍旧报错:“ cannot convert parameter 1 from 'class CString' to 'class _bstr_t' ”,是不是m_pConnection->Execute(strAction , &ra,adCmdText);中的参数转换问题?2.下面这段,在例子里可以好好运行,但稍加修改,放我的程序中就不行
int nItem;
try
{
while(!Rsc->adoEOF)
{
nItem=m_ctlResult.InsertItem(0xffff,_bstr_t(Rsc->GetCollect("_id")));
for(i=1;i<Rsc->Fields->Count;i++)
m_ctlResult.SetItem(nItem,i,1,_bstr_t(Rsc->Fields->GetItem(_variant_t((long)i))->Value),NULL,0,0,0 );
Rsc->MoveNext();
}
}
catch(_com_error e)//
{
AfxMessageBox("");///
}
其中Rsc就是RecordsetPtr型,我把它改为m_pRecordset(即我程序中的定义),但运行时出错。不知为什么。3.开始import应该在stdafx.h中,但
将#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
放在stdafx.h中没有反应,程序报错,找不到_ConnectionPtr、_RecordsetPtr等指针,只有放在主对话框的.h文件中,才能正常运行,不知为什么,有没有影响?4.还有这段也是在例子里可以好好运行,但修改后应用不行//插入记录
void CMyadotestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
_ConnectionPtr pConn;
if(FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create the Instance Failed!");
return;
} CString strSRC;
CString strSQL;
strSRC=GetAppPath(); strSRC=GetVFFreeTableSRCString(strSRC);
CString myname="xsc";
int myage=26;
float myhighth=1.72;
strSQL.Format("insert into mytable values('%s',%d,%f)",myname,myage,myhighth); //strSQL="insert into mytable(name,age,highth) values('xsc',24,1.72)"; _variant_t varSRC(strSRC);
_bstr_t bstrSRC(strSRC); if(FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("can not open the database!");
pConn.Release();
return;
} COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); try
{
pConn->Execute(_bstr_t(strSQL),&vtOptional,-1);
}
catch(...)
{
//pConn->Close();
//pConn->Release();
AfxMessageBox("inert the record error!");
return;
} pConn->Close();
pConn.Release(); AfxMessageBox("Insert the record successfully!");
}
其中strSRC=GetAppPath(); strSRC=GetVFFreeTableSRCString(strSRC);
系统报错为为undeclared identifier,why?
5.
不报错的,且在运行时不会出现“ Runtime Error!”只有一种,就是:
_variant_t ra;m_pConnection->Execute("INSERT INTO 表名(字段名1,字段名2,...) VALUES ('内容1','内容2',...)",&ra,adCmdText);//往表格里面添加记录
这里的内容1、内容2只能是常量、变量或表达式,不能是字符串。
为什么只有这一种能把数据加到数据库中,是不是前面程序问题?6.
加断点只知道哪有错误,不知道为什么有错误,还是知识差一些,所以加断点用处不大,能有什么别的好方法吗?7.
很多方法插入数据时,都会在运行时出现“ Runtime Error!”,不知是不是程序自身问题。唉,这个东西困扰很久了,一直搞不出来,被骂死了。恳请各位指点指点,谢谢!
(1)运行时错误,应该是你的pRecord获得的方法错了,使得pRecord是只读的,不能进行数据的更新。
(2)用 m_pConnection->Execute(strAction , &ra,adCmdText);时,要进行数据类型的转换;
m_pConnection->Execute(_bstr_t(strAction), &ra,adCmdText);2.
Rsc->Fields->GetItem(_variant_t((long)i))->Value可能是这个的错误,要看你查出来的记录段是不是空的,是空的话,用_bstr_t(..)就出错了。3.
-〉
CString str ;
str.Format("INSERT INTO 表名(字段名1,字段名2,字段名3,字段名4,...) VALUES ('%s', '%s','%s','%s',...", str1,str2,str3,str4,...);
m_pConnection->Execute(str,&ra,adCmdText);
一、是这样的,估计你没进行初始化。一般的用法是这样:
_RecordsetPtr pRecordset=NULL;
pRecordSet.CreateInstance(__uuidof(Recordset));
然后再pRecordSet->Open(...);
二、执行命令
可以先构造一个SQL字串,然后再执行。
我试过,有的数据库不支持在pConnection中执行命令,或要求参数不能为adCmdText,换成adExecuteNoRecord什么的。
我也在学习过程中ing.
中的__uuidof(Recordset什么意思啊
经过数据转换后,可以在把输入的数据写入数据库了,但经过转换后,字段主键的约束作用没有了。表现就是:可以输入一组数据,但主键为空,系统并不报错(以前会自动报错,表示“不能重复输入...”之类),在输入第二组数据时,系统报错,但直接退出应用程序。当第一组主键不为空且与以前内容不重复时,就可以成功输入,但再输入一组主键与之前相同的数据时,系统报错,且直接退出应用程序(与空主键之后一样)。而以前会在输入时直接判断、报错,不会退出应用程序。这个是怎么回事? 这个写入数据库过程还没用try...catch...,用一下看会不会恢复。我想问一下,如果使用if的话,要怎样写,可以使输入主键为空或重复时,弹出对话框表示不能进行?谢谢!还有问题请帮忙:
1.一开始初始化界面时,要载入数据,并在CListCtrl中显示出来,这个应该怎么样实现?
2.知道一组数据中的一个后,要进行数据的查询应该怎么进行?
谢谢!