数据库菜鸟问题, 请问怎么样可以向数据库刚插入一条记录后, 就得到该条记录的索引ID. 我不太懂数据库,我现在只能插入记录后,再用一条SQL语句把新记录的ID再给查出来,觉得烦,而且应该有更好的方法的.请问能不能插入的时候就返回该记录的ID啊? 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先得到这条记录的book,update之后再设定这个book,取得他的id 是sql server的话用 select @@IDENTITY as id to masterz: 老实说,不太懂.那个"book"是什么东西? 能不能稍微写两行代码.我现在的代码基本是这样的.m_pCommand->CommandText = "Insert ....."; m_pRecordset = m_pCommand->Execute(NULL, NULL,adCmdText); 不是sql server,是access,自己写的小应用. 设book没有成功,不设它照样去出来了,意料之外// mdbautoid.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "stdio.h"#include <tchar.h>#import "e:\Program Files\Common Files\System\ado\msado15.dll"\ no_namespace rename("EOF", "EndOfFile")int main(int argc, char* argv[]){ TCHAR conn_str_buf[1024]; LPCTSTR db_path=_T("C:\\temp\\test.mdb"); wsprintf(conn_str_buf,"PROVIDER=MSDASQL;DRIVER=" "{Microsoft Access Driver (*.mdb)};DBQ=%s;UID=;PWD=;",db_path); CoInitialize(NULL); try { _ConnectionPtr pConn("ADODB.Connection"); _RecordsetPtr pRst("ADODB.Recordset"); pConn->Open(conn_str_buf,"","",adConnectUnspecified); pRst->Open("table1", _variant_t((IDispatch *) pConn, true), adOpenKeyset, adLockOptimistic, adCmdTable); FieldsPtr fds=pRst->GetFields(); printf("printf field name of the table\n"); for(int i=0;i<fds->GetCount();i++) { FieldPtr fd=fds->GetItem(_variant_t(short(i))); printf("%s ",(LPCTSTR)fd->GetName()); } //_variant_t vBookMark = pRst->Book; pRst->AddNew(); pRst->Fields->Item["data"]->Value = (long)123456; pRst->Update(); //pRst->MoveFirst(); //pRst->Book = vBookMark; printf("AutoID is %d",(long)(pRst->Fields->Item["testid"]->Value)); pRst->MoveFirst(); pRst->Close(); pConn->Close(); } catch (_com_error &e) { printf("Description = '%s'\n", (char*) e.Description()); } ::CoUninitialize(); return 0;} 不好意思,没有马上回复,太热洗澡去了.我照着你给的代码, 象这样做为什么不行呢?我的m_pRecordSet也是_RecordsetPtr,按我的理解,m_pRecordset也应该是新插入的记录啊.为什么TRACE就错了.m_pCommand->CommandText = "insert into Device (EntryIP,IsRouter) Values ('127.0.0.10','1')"; m_pRecordset = m_pCommand->Execute(NULL, NULL,adCmdText); TRACE("AutoID is %d",(long)(m_pRecordset->Fields->Item["ID"]->Value));主要是按照你给的代码改,改动太大了. :) select 语句会返回记录集,insert into不会返回记录集 MFC+ADO 查询语句 日志为什么不能写 水晶报表动态设定数据库 格式转换 动态画图的问题,更多的是思路方面 怎么用API函数得到DateTimePicker的值 文件对话框的问题 这个error C2664怎么改啊? 应用程序正常初始化失败(Oxc0150002) 急急急!!!二叉排序树的建立中序遍历时总是遍历不了!!!在线等,谢谢 如何在内存中存取大概300张位图的数据(300×512×512)以便随时回放? 哪里可以找到微软TTS(text-to-speech)技术的文档?
老实说,不太懂.那个"book"是什么东西? 能不能稍微写两行代码.
我现在的代码基本是这样的.
m_pCommand->CommandText = "Insert .....";
m_pRecordset = m_pCommand->Execute(NULL, NULL,adCmdText);
// mdbautoid.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "stdio.h"
#include <tchar.h>
#import "e:\Program Files\Common Files\System\ado\msado15.dll"\
no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
TCHAR conn_str_buf[1024];
LPCTSTR db_path=_T("C:\\temp\\test.mdb");
wsprintf(conn_str_buf,"PROVIDER=MSDASQL;DRIVER="
"{Microsoft Access Driver (*.mdb)};DBQ=%s;UID=;PWD=;",db_path);
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open(conn_str_buf,"","",adConnectUnspecified);
pRst->Open("table1", _variant_t((IDispatch *) pConn, true),
adOpenKeyset, adLockOptimistic, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of the table\n");
for(int i=0;i<fds->GetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
//_variant_t vBookMark = pRst->Book;
pRst->AddNew();
pRst->Fields->Item["data"]->Value = (long)123456;
pRst->Update();
//pRst->MoveFirst();
//pRst->Book = vBookMark;
printf("AutoID is %d",(long)(pRst->Fields->Item["testid"]->Value));
pRst->MoveFirst(); pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}
我照着你给的代码, 象这样做为什么不行呢?我的m_pRecordSet也是_RecordsetPtr,
按我的理解,m_pRecordset也应该是新插入的记录啊.为什么TRACE就错了.m_pCommand->CommandText = "insert into Device (EntryIP,IsRouter) Values ('127.0.0.10','1')";
m_pRecordset = m_pCommand->Execute(NULL, NULL,adCmdText);
TRACE("AutoID is %d",(long)(m_pRecordset->Fields->Item["ID"]->Value));主要是按照你给的代码改,改动太大了. :)