用两个EDIT控件查询符合某一范围值的记录。两个EDIT控件,edit1和edit2,edit1代表下限值,edit2代表上限值,两者均为CString型。
sql.format("select *from tb_tool where  刀具长度 between '%s' and '%s'",edit1,edit2);
数据库中刀具长度这一列设置的是文本格式。
实际运行的时候iu,这一句为什么不起作用?
如果把刀具长度改成数字格式,然后改写
sql.format("select *from tb_tool where  刀具长度 between 10 and 100);
就可以了。
请各位大侠帮帮忙。

解决方案 »

  1.   

    把edit1和edit2转成数字呀.
    sql.format("select *from tb_tool where 刀具长度 between  %d and %d",atoi(edit1),atoi(edit2));
      

  2.   

    sql.format("select *from tb_tool where 刀具长度 between %s and %s",edit1,edit2);
      

  3.   

     '%s' and '%s'
    -------------------
    ‘ 逗号问题??
      

  4.   

    void CQtoolDlg::AddToGrid1()
    {
        ADOConn m_AdoConn;
    //打开数据库连接
    m_AdoConn.OnInitADOConn();
    CString sql,editll,editbl;
    m_editll.GetWindowText(editll);
    m_editbl.GetWindowText(editbl);
    sql.Format("select * from tool where 刀具长度 between '%d' and '%d'",atoi(editll),atoi(editbl));
    // sql.Format("select * from tool where 刀具长度 between 10 and 100");
    _RecordsetPtr m_pRecordset;
    //打开记录集
    m_pRecordset=m_AdoConn.GetRecordset((_bstr_t)sql);
    //遍历记录集
    while(m_AdoConn.m_pRecordset->adoEOF==0)
    {
    m_grid.InsertItem(0,"");//插入行
    //将记录集字段中的记录添加到List Control控件中
            m_grid.SetItemText(0,0,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具编号"));
    m_grid.SetItemText(0,1,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具类型"));
    m_grid.SetItemText(0,2,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具名称"));
    m_grid.SetItemText(0,3,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具材料"));
    m_grid.SetItemText(0,4,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具直径"));
    m_grid.SetItemText(0,5,(char*)(_bstr_t)m_pRecordset->GetCollect("底圆角半径"));
    m_grid.SetItemText(0,6,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具长度"));
    m_grid.SetItemText(0,7,(char*)(_bstr_t)m_pRecordset->GetCollect("刀刃长度"));
    m_grid.SetItemText(0,8,(char*)(_bstr_t)m_pRecordset->GetCollect("刀刃数"));
    m_grid.SetItemText(0,9,(char*)(_bstr_t)m_pRecordset->GetCollect("刀柄直径(T型刀)"));
    m_grid.SetItemText(0,10,(char*)(_bstr_t)m_pRecordset->GetCollect("螺距(螺纹铣刀)"));
    m_grid.SetItemText(0,11,(char*)(_bstr_t)m_pRecordset->GetCollect("刀具加工精度"));
    m_grid.SetItemText(0,12,(char*)(_bstr_t)m_pRecordset->GetCollect("刀柄形状"));
    //使记录集指针指向下一记录
    m_pRecordset->MoveNext();
    }
    //断开数据库连接
    m_AdoConn.ExitConnect();
    }
    以上是查询后的重新遍历所有记录的代码,我改成一楼大哥说的那样了,可是还不行。至于二楼的,我早就试过,不行,点击查询后,不显示记录。在按步运行按F10时,直接将该遍历函数跳过。