求助贴,如何判断变量的类型

风语空音 2018-01-02 11:16:19
用C++Builder写了个小程序,用来查找数据库中值为“Edit1->Text”的数据,但是Text的数据类型是不确定的,如果数据类型不对,我的SQL语句会执行出错,所以写了个条件语句来判断,但是这条件语句好像没啥用,求大佬帮忙解决一下
void __fastcall TForm2::Button1Click(TObject *Sender)
{
ADOQuery2->SQL->Clear();
int i=ComboBox1->ItemIndex;
//Caption=IntToStr(i); //测试用
if(i<0) //i==-1
{
ShowMessage("下拉列表请选择一项");
}
else
{
AnsiString ss;
ss=ComboBox1->Text;
AnsiString aa=Edit1->Text;
if(typeid(aa)==typeid(int)) //就是这个条件语句
{
ADOQuery2->SQL->Add(" select * from yuangong where "+ss+"="+Edit1->Text+";");
}
else
{
ADOQuery2->SQL->Add(" select * from yuangong where "+ss+"='"+Edit1->Text+"';");
}
ADOQuery2->Open();
if(ADOQuery2->RecordCount>0)
{
ShowMessage("查找成功");

}
else
{
ShowMessage("没有找到该员工,请确认该员工是否存在。");
}
}
}

用了个TypeId在头文件声明了#include <typeinfo>也不知道这样声明对不对。
还有就是写了个登录页面,登录时老是要填数据库密码(我不想填密码),我不想用ADO控件就自己写了一个,
估计写的有点问题,也请大佬们一并看看。
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Label4->Caption=Now();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit1->Clear();
Edit2->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{//登录程序主要就是这一段代码
myConnection=new TADOConnection(NULL);

myConnection->ConnectionString="Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=XXB-4F5B7C16ABD;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=XXB-4F5B7C16ABD;Use Encryption for Data=False;Tag with column collation when possible=False";
myConnection->Open();
myQuery=new TADOQuery(NULL);
myQuery->Connection=myConnection;
if(RadioButton1->Checked!=true&&RadioButton2->Checked!=true)
{
ShowMessage("请选择身份类型");
}
else
{
if(RadioButton1->Checked==true)
{
myQuery->SQL->Clear();
myQuery->SQL->Add("select * from denglu where username='"+Edit1->Text+"' and password='"+Edit2->Text+"'");
myQuery->Open();
if(myQuery->FieldByName("password")->AsString==""||myQuery->FieldByName("right")->AsString=="0")
{
ShowMessage("登录名或密码错误请重新输入!");
}
else
{
Form1->Hide();
Form2->Show();
}
}
if(RadioButton2->Checked==true)
{
myQuery->SQL->Clear();
myQuery->SQL->Add("select * from denglu where username='"+Edit1->Text+"' and password='"+Edit2->Text+"'");
myQuery->Open();
if(myQuery->FieldByName("password")->AsString==""||myQuery->FieldByName("right")->AsString=="1")
{
ShowMessage("登录名或密码错误请重新输入!");
}
else
{ sss=myQuery->FieldByName("id")->AsString;
Form1->Hide();
Form3->Show();
}
}
}
}
//---------------------------------------------------------------------------

...全文
624 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooolinux 2018-01-04
  • 打赏
  • 举报
回复
int i=temp.ToIntDef(0); 结果为0的时候会无法判断是本来输入0,还是字符串非数字。
ooolinux 2018-01-03
  • 打赏
  • 举报
回复
就算你在Edit1中输入123这样的数字,Edit1->Text就是等于"123",注意是加引号的,它就是字符串类型。 构造SQL语句的时候,因为SQL语句也是字符串类型,所以可以把Edit1->Text和字符串直接相加。 比如这样写: AnsiString ss; ss= "Select 日产液量 from AllRec where 井号='1号井'"; //构造sql的语句单独写一行 ShowMessage(ss); //显示一下ss看sql语句是不是构造正确,以后注释掉这行 ADOQuery1->Close(); //这行很必要 ADOQuery1->SQL->Clear(); ADOQuery1->SQL->Add(ss); ADOQuery1->Open(); // select等返回结果集的语句用Open(),插入、删除、更新等语句用ADOQuery1->ExecSQL();
风语空音 2018-01-03
  • 打赏
  • 举报
回复
引用 1 楼 ooolinux的回复:
Edit1->Text 的类型都是AnsiString(6.0版本)或者UnicodeString类型(2009以后版本) Edit1->Text .ToInt() 或者 StrToInt(Edit1->Text ) 可以把字符串转换为int类型,还有ToDouble转换为double类型。 ADOConnection1的LoginPrompt 设置为false 可以不用输入密码。
我不是要把Text的类型变成某一类,Text的值是使用者输入的,我也不确定是什么类型,所以想通过判断Text的类型改变相应的SQL语句
ooolinux 2018-01-03
  • 打赏
  • 举报
回复
改成: catch(EConvertError &e) 好一点,捕获转换异常,因为你的代码中还有可能抛出别的异常,catch(...) 会捕获所有异常。
ooolinux 2018-01-03
  • 打赏
  • 举报
回复
int i=temp.ToIntDef(0); 你这样写可以的,也可以用try:
    try
    {
        int i=Edit1->Text.ToInt(); //如果无法转换为整数就会抛出异常
		代码。。。
    }
    catch(...)
    {
        ShowMessage("请输入数字");
    }
风语空音 2018-01-03
  • 打赏
  • 举报
回复
老哥,你是没明白我意思,条件组合我也会,我的意思数据库里员工那张表的数据有int型、float型、nchar型等, 我如果通过查id(数据类型是int型)查员工信息的,由使用者输入员工id(Edit->Text),但是使用者可能不按规则输入 比如输入”42@#ss“这样我用 ADOQuery2->SQL->Add(" select * from yuangong where "+ss+"="+Edit1->Text+";");语句查询的 时候程序就会因为数据类型问题出错,我这写了个代码用来解决类型问题感觉有点复杂,大佬帮我看看有什么可以改改,简化一下
void __fastcall TForm2::Button1Click(TObject *Sender)
{
           ADOQuery2->SQL->Clear();
        int i=ComboBox1->ItemIndex;
    //Caption=IntToStr(i); //测试用
    if(i<0) //i==-1
    {
        ShowMessage("下拉列表请选择一项");
    }
        else
        {
        AnsiString ss;
        ss=ComboBox1->Text;
          if(ss=="id")
          {
          AnsiString temp=Edit1->Text;
          int i=temp.ToIntDef(0);
          if (i==0)   //如果Text不是int型
          {
            ShowMessage("没有找到该员工,请确认该员工是否存在。");

          }

            else

      { ADOQuery2->SQL->Add(" select * from yuangong where "+ss+"='"+Edit1->Text+"';");

    ADOQuery2->Open();
    if(ADOQuery2->RecordCount>0)
      {
            ShowMessage("查找成功");

      }
      else
      {
          ShowMessage("没有找到该员工,请确认该员工是否存在。");
    }
    }
   }
   else
      { ADOQuery2->SQL->Clear();
       ADOQuery2->SQL->Add(" select * from yuangong where "+ss+" like '%"+Edit1->Text+"%';");

    ADOQuery2->Open();
    if(ADOQuery2->RecordCount>0)
      {
            ShowMessage("查找成功");

      }
      else
      {
          ShowMessage("没有找到该员工,请确认该员工是否存在。");
    }
    }
  }
}
ooolinux 2018-01-03
  • 打赏
  • 举报
回复
貌似你要的是数据库条件组合查询,可以用多个Edit,一个Edit对应一种条件。
以下CB代码参考一下吧,刚学时学书里做的,控件都没有命名。

AnsiString sql="select * from 员工信息表 where";
AnsiString str="";
if(ComboBox3->Text=="是")
    str+=" 是否管理人员=true and";
else if(ComboBox3->Text=="否")
    str+=" 是否管理人员=false and";
if(Edit1->Text!="")
    str+=" 员工号='"+Edit1->Text+"' and";
if(Edit2->Text!="")
    str+=" 姓名='"+Edit2->Text+"' and";
if(ComboBox1->Text!="")
    str+=" 性别='"+ComboBox1->Text+"' and";
if(Edit3->Text!="")
    str+=" 出生日期=#"+Edit3->Text+"# and";
if(ComboBox2->Text!="")
    str+=" 学历='"+ComboBox2->Text+"' and";
if(Edit4->Text!="")
    str+=" 电话='"+Edit4->Text+"' and";
if(ComboBox4->Text!="")
    str+=" 部门='"+ComboBox4->Text+"' and";
if(ComboBox5->Text!="")
    str+=" 职位='"+ComboBox5->Text+"' and";
str+=" year(出生日期)>1900"; //"  1=1" 应该也可以
sql+=str;
ooolinux 2018-01-03
  • 打赏
  • 举报
回复
参考3楼的代码。
风语空音 2018-01-03
  • 打赏
  • 举报
回复
明白了一点,不过我这一段代码执行起来还是有点问题 AnsiString ss;         ss=ComboBox1->Text;        ADOQuery2->SQL->Add(" select * from yuangong where "+ss+"="+Edit1->Text+";");        ADOQuery2->Open(); 如果变量ss是id的话(我的id是int型的,其他的都是vhar型的),在Edit1->Text输入djd等字符型数据就会出现无效的列名、无法将djd转换为int型, 不知有没有好的方法,还有就是有没有什么函数可以返回Edit1->Text的类型。
ooolinux 2018-01-02
  • 打赏
  • 举报
回复
Edit1->Text 的类型都是AnsiString(6.0版本)或者UnicodeString类型(2009以后版本) Edit1->Text .ToInt() 或者 StrToInt(Edit1->Text ) 可以把字符串转换为int类型,还有ToDouble转换为double类型。 ADOConnection1的LoginPrompt 设置为false 可以不用输入密码。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧