qq_36766545 2017-06-04 07:57 采纳率: 0%
浏览 1618
已结题

各位大佬,请问一下:VC++6.0编译时没错误,但是运行不了是因为什么啊,都是照着书编的!

BOOL CRentDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
        pSysMenu->AppendMenu(MF_SEPARATOR);
        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

// TODO: Add extra initialization here
//初始化List控件的栏目名,设置List控件每列的标题、宽度
m_rentInfoList.InsertColumn(0,"ID",LVCFMT_LEFT,40,-1);
m_rentInfoList.InsertColumn(1,"租借人",LVCFMT_LEFT,160,-1);
m_rentInfoList.InsertColumn(2,"租借DVD名",LVCFMT_LEFT,200,-1);
m_rentInfoList.InsertColumn(3,"租借日期",LVCFMT_LEFT,160,-1);
//设置List的行被选中时是全行选中
m_rentInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//设置Date Time Picker控件的日期格式
m_DateFrom.SetFormat("yyyyMMdd");
m_DateTo.SetFormat("yyyyMMdd");
//日期查询初始化时是不选中的,设置起始日期的控件不可用
m_check_Date=FALSE;
m_DateFrom.EnableWindow(FALSE);
m_DateTo.EnableWindow(FALSE); 
//初始化数据库连接
HRESULT  hr;
try
{
    //实例化连接对象
    hr=m_pConnection.CreateInstance(__uuidof(Connection));
    if(SUCCEEDED(hr))
    {
        //设置连接串属性为UDL文件
         m_pConnection->ConnectionString="File Name=my_data1.udl";
         //设置等待连接打开的时间为20秒
          m_pConnection->ConnectionTimeout=20;
          hr=m_pConnection->Open ("","","",NULL);
          if(FAILED(hr))
          {
              AfxMessageBox("open fail!");
              return TRUE;
          }
    }
    else
    {
              AfxMessageBox("createinstance of Connection fail!");
              return TRUE;
    }
} 
catch(_com_error e)
{//给出异常信息
    _bstr_t bstrSource(e.Source ());
    _bstr_t bstrDescription(e.Description());
    AfxMessageBox(bstrSource+bstrDescription);
    return TRUE;
}
//获得本月的出租记录,并显示在List控件中
InitListCtr();
return TRUE;  // return TRUE  unless you set the focus to a control

}

void CRentDlg::OnCheckDate()
{
// TODO: Add your control notification handler code here
if(m_check_Date==FALSE)
{
m_check_Date=TRUE;
m_DateFrom.EnableWindow(TRUE);
m_DateTo.EnableWindow(TRUE);
}
else
{ m_check_Date=FALSE;
m_DateFrom.EnableWindow(FALSE);
m_DateTo.EnableWindow(FALSE);
}
}

void CRentDlg::InitListCtr()
{
HRESULT hr;
//定义记录集指针
RecordsetPtr pRentRecordset;
//实例化记录集指针
hr=pRentRecordset.CreateInstance(
_uuidof(Recordset));
//判断创建记录集指针实例是否成功
if(FAILED(hr))
{
AfxMessageBox("createinstance of Recordset fail!\n can't initiate List control!");
return;
}
//定义字符串存储SQL语句
CString strSql;
//定义_variant_t变量存储从数据库读取到的字段
_variant_t var;
//定义字符串存储_varient_t变量中的字符串
CString strValue;
//List控件中记录的序号
int curItem=0;
//初始化SQL语句字符串,获得tbRentInfo表中的
strSql="SELECT * FROM tbRentInfo";
try
{//利用Open函数执行SQL命令,获得查询结果记录集
//需要把CString类型转化为_varient_t类型
hr=pRentRecordset->Open (_variant_t(strSql),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hr))
{//判断记录集是否到末尾,对每条记录,把字段插入List控件的每一行中
while(!pRentRecordset->adoEOF)
{//获得记录集中但前记录的第一个字段的值
var = pRentRecordset->GetCollect((long)0);
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
//插入该字符串到List控件中
m_rentInfoList.InsertItem(curItem,strValue);
//获得记录集中但前记录的"Name"字段的值
var = pRentRecordset->GetCollect("Name");
if(var.vt != VT_NULL);
strValue = (LPCSTR)_bstr_t(var);
m_rentInfoList.SetItemText(curItem,1,strValue);

               var = pRentRecordset->GetCollect("DVDID");

               if(var.vt != VT_NULL);
                   strValue = (LPCSTR)_bstr_t(var);
                   //需要根据该DVDID查询tbDVDInfo,获得DVDname,插入List控件中
              m_rentInfoList.SetItemText(curItem,2,QueryDVDName(strValue));

              var = pRentRecordset->GetCollect("Date");
              if(var.vt != VT_NULL);
                   strValue = (LPCSTR)_bstr_t(var);
            m_rentInfoList.SetItemText(curItem,3,strValue);
            //移动当前记录到下一条记录
            pRentRecordset->MoveNext();
            curItem++;
           }
       }
       else
       {
           AfxMessageBox("Open recordset fail!");
       }
       }
       catch(_com_error *e)
       {
           AfxMessageBox( e->ErrorMessage());
           return;
       }
       pRentRecordset->Close();
       pRentRecordset=NULL;

}

CString CRentDlg::QueryDVDName(CString DVDID)
{
RecordsetPtr pDVDNameRecordset;
pDVDNameRecordset.CreateInstance(
_uuidof(Recordset));
CString strValue;
_variant_t var;
//直接使用_bstr_t类型的字符串
_bstr_t vSQL;
//选择参数指定的DVDID的记录
vSQL="select DVDNme from tbDVDInfo where DVDID="+DVDID;
try
{//vSQL可以直接作为参数传给Open函数,而如果使用CStrig类型需要类型转换
HRESULT hr;
//利用SQL语句打开记录集
hr=pDVDNameRecordset->Open(vSQL,
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(!FAILED(hr))
{
var=pDVDNameRecordset->GetCollect("DVDName");
if(var.vt != VT_NULL);
strValue = (LPCSTR)_bstr_t(var);
}
else strValue="";
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
strValue="";

 }
 pDVDNameRecordset->Close();
 pDVDNameRecordset=NULL;
 return strValue;

}
这些是我加的除了添加成员函数和成员变量以外的程序,应该怎么改啊

  • 写回答

3条回答 默认 最新

  • threenewbee 2017-06-04 18:01
    关注

    m_pConnection->ConnectionString="File Name=my_data1.udl";
    你的odbc怎么配置的。

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办