基于_RecordSetPtr封装了一个CAdoRecordSet类
将一些操作封装了起来
运行功能正常
但是在下面的函数中CAdoRecordSet CInsertDlg::InitRecordSet()
{ //打开数据连接
try
{
CAdoConnection pConn;
pConn.ConnectSQLServer("(local)","VCADO1","sa","sql",adModeUnknown); CAdoRecordSet ar1;
ar1.SetAdoConnection(&pConn);
ar1.SetCursorLocation();
ar1.Open("student", adCmdTable);
pConn.BeginTrans(); CGridDlg gd1; //这里是直接调用含有DataGrid的一个
//对话框
gd1.m_adoRecordSet=&ar1;
gd1.DoModal();
return ar1;
pConn.Close();
}
catch (_com_error e)
{
MessageBox(e.ErrorMessage(), "Error", MB_ICONERROR | MB_OK);
}
return NULL;
}
在这个函数中直接调用含有DataGrid的一个对话框就可以将检索到的数据显示出来,
但是如果分成两个函数:void CInsertDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CAdoRecordSet recordset1;
recordset1=InitRecordSet();
CGridDlg gd1;
gd1.m_adoRecordSet=&recordset1;
gd1.DoModal();}CAdoRecordSet CInsertDlg::InitRecordSet()
{ //打开数据连接
try
{
CAdoConnection pConn;
pConn.ConnectSQLServer("(local)","VCADO1","sa","sql",adModeUnknown); CAdoRecordSet ar1;
ar1.SetAdoConnection(&pConn);
ar1.SetCursorLocation();
ar1.Open("student", adCmdTable);
pConn.BeginTrans();
//这里去掉了含有DataGrid的对话框的显示部分
//将它的显示转移到了OnButton1()函数中 return ar1;
pConn.Close();
}
catch (_com_error e)
{
MessageBox(e.ErrorMessage(), "Error", MB_ICONERROR | MB_OK);
}
return NULL;
}上面的代码将含有DataGrid的对话框的显示部分转移到了OnButton1()函数中
可是运行结果是DataGrid中没有记录,而且连表头都没有初始化,测试了一下,
发现返回的是个空的记录集合,而且执行当中没有出现错误提示,
这到底是为什么哪,难道我的CAdoRecordSet类的实例不能作为对象返回吗?
将一些操作封装了起来
运行功能正常
但是在下面的函数中CAdoRecordSet CInsertDlg::InitRecordSet()
{ //打开数据连接
try
{
CAdoConnection pConn;
pConn.ConnectSQLServer("(local)","VCADO1","sa","sql",adModeUnknown); CAdoRecordSet ar1;
ar1.SetAdoConnection(&pConn);
ar1.SetCursorLocation();
ar1.Open("student", adCmdTable);
pConn.BeginTrans(); CGridDlg gd1; //这里是直接调用含有DataGrid的一个
//对话框
gd1.m_adoRecordSet=&ar1;
gd1.DoModal();
return ar1;
pConn.Close();
}
catch (_com_error e)
{
MessageBox(e.ErrorMessage(), "Error", MB_ICONERROR | MB_OK);
}
return NULL;
}
在这个函数中直接调用含有DataGrid的一个对话框就可以将检索到的数据显示出来,
但是如果分成两个函数:void CInsertDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CAdoRecordSet recordset1;
recordset1=InitRecordSet();
CGridDlg gd1;
gd1.m_adoRecordSet=&recordset1;
gd1.DoModal();}CAdoRecordSet CInsertDlg::InitRecordSet()
{ //打开数据连接
try
{
CAdoConnection pConn;
pConn.ConnectSQLServer("(local)","VCADO1","sa","sql",adModeUnknown); CAdoRecordSet ar1;
ar1.SetAdoConnection(&pConn);
ar1.SetCursorLocation();
ar1.Open("student", adCmdTable);
pConn.BeginTrans();
//这里去掉了含有DataGrid的对话框的显示部分
//将它的显示转移到了OnButton1()函数中 return ar1;
pConn.Close();
}
catch (_com_error e)
{
MessageBox(e.ErrorMessage(), "Error", MB_ICONERROR | MB_OK);
}
return NULL;
}上面的代码将含有DataGrid的对话框的显示部分转移到了OnButton1()函数中
可是运行结果是DataGrid中没有记录,而且连表头都没有初始化,测试了一下,
发现返回的是个空的记录集合,而且执行当中没有出现错误提示,
这到底是为什么哪,难道我的CAdoRecordSet类的实例不能作为对象返回吗?
{
// TODO: Add your control notification handler code here
CAdoRecordSet recordset1;
recordset1=InitRecordSet();
// *** recordset1=InitRecordSet(); 你这样直接赋值,如果InitRecordSet()函数返回
// *** 的对象有里面指针的话,意思是说,假如对象里有个变量 char* str;
// *** str指向的内容就会因直接赋值而释放出错,str变量被释放两次.
// *** 第一次,InitRecordSet();结束,str变量指向的内存被释放,
// *** 第二次,recordset1对象结束,str变量指向的内存被释放,其实这次可能释放的是系统区的内存
CGridDlg gd1;
gd1.m_adoRecordSet=&recordset1;
gd1.DoModal();}CAdoRecordSet CInsertDlg::InitRecordSet()
{
.... // *** 程序体
return ar1;
pConn.Close(); // *** 这里永远都还会执行,上面已经return 了}这个函数最好改为指针方式:
CAdoRecordSet* CInsertDlg::InitRecordSet()
{
.... // *** 程序体
}
// *** 希望能帮点忙 ***