小生近日参考了一个师兄编的程序.在VC中输入了一个ADO+SQL2000的例子。是关于员工信息管理的,结果在我的电脑上自己建了一个数据库,相连时,调试是碰到了一大堆问题。望各位多多帮忙!! 
    问题是这样的:     我在SQL中建了一个表Employees(Emp_id,Emp_name,Sex,Title,Wage,FPDate,Dep_id),Emp_id设为主键,标识:是,标识递增量:1. (师兄的表就6个列Emp_name,Sex,Title,Wage,FPDate,Dep_id 对应一个员工的姓名,性别,编号,出生年月,指纹特征值,后来在添加到数据库是出现Emp_id无效,我就把添加了Emp_id列,并设为主键)然后为该表创建了CEmployees类,数据成员和表对应分别是 int Emp_id;CString Emp_name;CString Sex;CString Title;CString Wage;int Dep_id.  
 
    通过对话框类CEditDlg像表中输入员工的信息:
EditDlg.h中:
public:CString cEmpId;CString strSex;
enum { IDD = IDD_EDIT_DIALOG };
CComboBox  m_Sex;
CString m_EmpName;
CString m_Title;
CString m_Wage;
Employees.cpp中://根据员工编号读取所有字段值
void CEmployees::GetData(CString cEmpId)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn(); //设置SELECT语句
_bstr_t vSQL;  
vSQL = "SELECT * FROM Employees WHERE Emp_id=" + cEmpId; //执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);//GetRecordSet执行查询,返回记录集m_pRecordset //返回各列(称为字段)的值
if (m_pRecordset->adoEOF == 1)//任何情况下adoEOF不会等于1,要么是0,要么是-1
CEmployees();
else
{
Emp_id = atoi(cEmpId); ???这里为什么啊?
Emp_name = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Emp_Name");
Sex = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Sex");
Title = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Title");
Wage  = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Wage");
Dep_id = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Dep_id"));
}
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
//使用SELECT语句返回部门表中最大的Emp_id字段值,并以此作为新记录的编号值返回。
int CEmployees::GetMaxUserID()
{
    int MaxUserID=0;
    ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
    _bstr_t vSQL;
vSQL = "SELECT max(Emp_id) as Emp_id  FROM Employees" ;//AS子句可用来更改结果集列名或为导出列指定名称
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);//执行查询并返回记录集 if (m_pRecordset->adoEOF == 1)
{
MaxUserID=0;
}
else
{
MaxUserID=atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Emp_id"));
}
m_AdoConn.ExitConnect();    return MaxUserID;}请问:vSQL = "SELECT max(Emp_id) as Emp_id  FROM Employees" ;为什么要返回max(Emp_id)???
Emp_id是保存当前编辑的记录所属员工的编号
cEmpId是保存当前记录的编号最后运行VC出现Runtime Error错误:This application has requested,theRuntime to terminate it is an unusual way.Please contact the application's support team for more information. 
但是记录添加到表中,师兄的表中没有Emp_id列,也可以添加记录???这是为什么呀???

解决方案 »

  1.   

    Emp_id = atoi(cEmpId); ???这里为什么啊? 
    什么意思?
    atoi
    字符转成int啊,
      

  2.   

    是的,int Emp_id;
         CString CEmpId;
      

  3.   

    Emp_id = atoi(cEmpId); //意思的是将字符串变量cEmpId的值转换成整形变量值Emp_id
    原因很简单:因为你的表Employees 里面的字段Emp_id是整形的,你看看。如果你不Emp_id = atoi(cEmpId);,你们就和你的数据表里面的字段的类型不匹配,肯定会报错误!
      

  4.   

       最后运行VC出现Runtime Error错误:This application has requested,theRuntime to terminate it is an unusual way.Please contact the application's support team for more information. 
    这个问题怎么解决???上火
      

  5.   

    atoi(cEmpId.GetBuffer(0)),这个函数的方法查看MSDN吧,上门很详细的