求助 这是我的mfc下一个修改数据库信息的一段代码,里面执行到m_Ado.m_pRecordset->Update(); 这句话的时候就会出现异常,而且修改不了数据库内的信息。

来源:互联网 发布:上海程序员工资一般多少 编辑:程序博客网 时间:2024/06/06 05:27
void CStuInfoDlg::OnClickedButtonAlter()

{

UpdateData(true);
ADO m_Ado;
CString str;
int nItem = m_ListStudent.GetNextSelectedItem(possion);
str.Format("%s",m_ListStudent.GetItemText(nItem,0)); 
CString msg;
msg.Format("你确定要修改学号:%s的信息?",str);
m_Ado.OnInitADOConn();
    CString sql;
sql.Format("update Students \
   set Student_Sno ='%s',Student_Sname = '%s',Student_Sex = '%s',Departments_Dno = '%s' \
   where Student_Sno='%s'",m_Sno,m_Sname,m_Sex,m_Department,str);

m_Ado.m_pRecordset = m_Ado.OpenRecordset(sql); //打开记录集
if ( AfxMessageBox(msg,MB_YESNO) == IDYES)
{

if ( m_Ado.ExcuteSQL((_bstr_t)sql) )
{
MessageBox("恭喜,操作成功!");
m_Ado.m_pRecordset->Update();      //更新记录集   //出现异常………………
   m_Ado.CloseRecordset();        //关闭记录集
           m_Ado.CloseConn();          //断开数据库连接
}
else
{
MessageBox("修改失败,请核查");
return;
}
 


}
AddToGrid();//函数原型在下面(没错)
}

void CStuInfoDlg::AddToGrid(void)
{
ADO m_Ado;
m_Ado.OnInitADOConn();//连接数据库


    CString SQL = "select * from Students  order by Student_Sno asc";
//设置查询字符串
m_Ado.m_pRecordset = m_Ado.OpenRecordset(SQL);//打开记录集
while(!m_Ado.m_pRecordset->adoEOF)
{
m_ListStudent.InsertItem(0," ");  //向列表视图控件中插入行
//向列表视图中插入列
m_ListStudent.SetItemText(0,0,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("Student_Sno"));
m_ListStudent.SetItemText(0,1,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("Student_SName"));
m_ListStudent.SetItemText(0,2,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("Student_Sex"));
m_ListStudent.SetItemText(0,3,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("Departments_Dno"));

m_Ado.m_pRecordset->MoveNext();//将记录集指针移动到下一条记录
}
//m_Ado.m_pRecordset->Update();
m_Ado.CloseRecordset();
m_Ado.CloseConn();//断开数据库连接
}