初级数据库编程问题,刚接触这个,不太会啊!
问题1:为何以下代码执行多次(就是多次按了这个Button)会出现死机情况?这些代码是一个Button的OnClick事件触发的。
代码如下:
void CADODemoDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
m_list.DeleteAllItems();
m_pConnection.CreateInstance(_uuidof(Connection)); //初始化Connection指针
m_pRecordset.CreateInstance(_uuidof(Recordset));//初始化Recordset指针
try
{
m_pConnection->Open("DSN=MS Access Database","","",0);
m_pRecordset->Open("SELECT * FROM 学生信息", // 查询表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
_variant_t var;
CString strName,strNum,strSex,strAge;
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
VERIFY("表内数据为空");
}
// 读入库中各字段并加入列表框中
int i=0;
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("学号");
if(var.vt != VT_NULL)
strNum = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("姓名");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("年龄");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("性别");
if(var.vt != VT_NULL)
strSex = (LPCSTR)_bstr_t(var);
m_list.InsertItem(i,strNum);
m_list.SetItemText(i,1,strName);
m_list.SetItemText(i,2,strAge);
m_list.SetItemText(i,3,strSex);
i++;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
m_pConnection= NULL;
m_pRecordset=NULL;
}
问题2:我家里系统是XP专业版的,为什么装不上sql2000?安装的时候,他提示我说此系统上无法安装服务器端,只能安装客户端。结果导致我一直无法建立数据库。