那位遇到过这种问题?怎么解决的

chilicsdn 2003-11-19 10:22:34
我写了一个带参数的查询,如果Deal_Man传入中文则查询语句截掉一部分,而英文则正常,我想应该是unicode编码的问题,但我的Deal_Man是通过TEdit的值传入的,怎样解决?我用的是ado2.8版本,希望得到大家帮忙!!!!
具体语句:
...
StrSql:='select * from Workflow_operation c where c.bill_num in (';
StrSql:=StrSql+' select distinct a.bill_num from workflow_bill a,workflow_detail_bill b';
StrSql:=StrSql+' where a.bill_num=b.bill_num ';
StrSql:=StrSql+' and b.deal_man like :Deal_Man ';
StrSql:=StrSql+' and a.bill_class like :Bill_Class';
StrSql:=StrSql+' and a.alarm_class like :Alarm_Class';
if (cxDEdtStart_Date_Begin.Text<>'') and (cxDEdtStart_Date_End.Text<>'') then
begin
StrSql:=StrSql+' and a.start_date between :start_date_begin and :start_date_end';
end;
if (cxDEdtComp_Date_Begin.Text<>'') and (cxDEdtComp_Date_End.Text<>'') then
begin
StrSql:=StrSql+' and a.Actual_Comp_date between :Comp_date_begin and :Comp_date_end';
end;
StrSql:=StrSql+' )';
传值:
if Trim(EdtDeal_Man.Text)<>'' then
begin
// ADOQuery1.Parameters.ParamByName('Deal_Man').Value:=Trim(EdtDeal_Man.Text)
DealMan:=Trim(EdtDeal_Man.Text);
ADOQuery1.Parameters.ParamByName('Deal_Man').DataType:=ftString;
ADOQuery1.Parameters.ParamByName('Deal_Man').Value:=DealMan;
end
else
ADOQuery1.Parameters.ParamByName('Deal_Man').Value:='%%';
if Trim(CobBill_Class.Text)<>'' then
ADOQuery1.Parameters.ParamByName('Bill_Class').Value:=Trim(CobBill_Class.Text)
else
ADOQuery1.Parameters.ParamByName('Bill_Class').Value:='%%';
if Trim(CobAlarm_Class.Text)<>'' then
ADOQuery1.Parameters.ParamByName('Alarm_Class').Value:=Trim(CobAlarm_Class.Text)
else
ADOQuery1.Parameters.ParamByName('Alarm_Class').Value:='%%';
if (cxDEdtStart_Date_Begin.Text<>'') and (cxDEdtStart_Date_End.Text<>'') then
begin
ADOQuery1.Parameters.ParamByName('start_date_begin').Value:=cxDEdtStart_Date_Begin.Date;
ADOQuery1.Parameters.ParamByName('start_date_end').Value:=cxDEdtStart_Date_End.Date;
end;

if (cxDEdtComp_Date_Begin.Text<>'') and (cxDEdtComp_Date_End.Text<>'') then
begin
ADOQuery1.Parameters.ParamByName('Comp_date_begin').Value:=cxDEdtComp_Date_Begin.Date;
ADOQuery1.Parameters.ParamByName('Comp_date_end').Value:=cxDEdtComp_Date_End.Date;
end;
在sqlserver时间跟踪器:
传参数‘杨测试’
exec sp_executesql N'select * from Workflow_operation c where c.bill_num in ( select distinct a.bill_num from workflow_bill a,workflow_detail_bill b where a.bill_num=b.bill_num and b.deal_man like @P1 and a.bill_class like @P2 and a.alarm_class like @P3 )
', N'@P1 varchar(3),@P2 varchar(2),@P3 varchar(2)', '杨
传参数‘test’
exec sp_executesql N'select * from Workflow_operation c where c.bill_num in ( select distinct a.bill_num from workflow_bill a,workflow_detail_bill b where a.bill_num=b.bill_num and b.deal_man like @P1 and a.bill_class like @P2 and a.alarm_class like @P3 )
', N'@P1 varchar(4),@P2 varchar(2),@P3 varchar(2)', 'test', '%%', '%%'
...全文
22 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonylk 2003-11-19
  • 打赏
  • 举报
回复
我曾经有个程序是这样做unicode查询的:

procedure TMLDataModule.SearchMusic(MusicName,AlbumName,ArtistName:WideString);
begin
...
sql:=' (music_name LIKE ''%'' + ' + EncodeUnicodeSQL(MusicName) + ' + ''%'')';
...
end;

function EncodeUnicodeSQL(Value:WideString):String;
//encode the widestring to string of unicode ascii set
//be used for search unicode char in SQL server
//2003.03.21
var
I:Integer;
begin
Result:='';
for I:=1 to Length(Value) do begin
if I>1 then begin
Result:=Result+'+';
end;
Result:=Result+'NCHAR('+IntToStr(Word(Value[I]))+')';
end;
end;
chilicsdn 2003-11-19
  • 打赏
  • 举报
回复
我这样写正常,上面那样不行 真是搞不懂!!!
StrSql:='select bill_type, Count(distinct a.bill_num) as Count from workflow_bill a,workflow_detail_bill b';
StrSql:=StrSql+' where a.bill_num=b.bill_num ';
StrSql:=StrSql+' and b.deal_man like :Deal_Man ';
StrSql:=StrSql+' and a.bill_class like :Bill_Class';
StrSql:=StrSql+' and a.alarm_class like :Alarm_Class';
if (cxDEdtStart_Date_Begin.Text<>'') and (cxDEdtStart_Date_End.Text<>'') then
begin
StrSql:=StrSql+' and a.start_date between :start_date_begin and :start_date_end';
end;
if (cxDEdtComp_Date_Begin.Text<>'') and (cxDEdtComp_Date_End.Text<>'') then
begin
StrSql:=StrSql+' and a.Actual_Comp_date between :Comp_date_begin and :Comp_date_end';
end;
StrSql:=StrSql+' group by bill_type order by bill_type';
end;
try
// TmpQuery:=TADOQuery.Create(self);
TmpQuery.Connection:=ADOConnection1;
TmpQuery.Close;
TmpQuery.SQL.Clear;
TmpQuery.SQL.Add(StrSql);
if Trim(EdtDeal_Man.Text)<>'' then
TmpQuery.Parameters.ParamByName('Deal_Man').Value:=Trim(EdtDeal_Man.Text)
else
TmpQuery.Parameters.ParamByName('Deal_Man').Value:='%%';
if Trim(CobBill_Class.Text)<>'' then
TmpQuery.Parameters.ParamByName('Bill_Class').Value:=Trim(CobBill_Class.Text)
else
TmpQuery.Parameters.ParamByName('Bill_Class').Value:='%%';
if Trim(CobAlarm_Class.Text)<>'' then
TmpQuery.Parameters.ParamByName('Alarm_Class').Value:=Trim(CobAlarm_Class.Text)
else
TmpQuery.Parameters.ParamByName('Alarm_Class').Value:='%%';
if (cxDEdtStart_Date_Begin.Text<>'') and (cxDEdtStart_Date_End.Text<>'') then
begin
TmpQuery.Parameters.ParamByName('start_date_begin').Value:=cxDEdtStart_Date_Begin.Date;
TmpQuery.Parameters.ParamByName('start_date_end').Value:=cxDEdtStart_Date_End.Date;
end;

if (cxDEdtComp_Date_Begin.Text<>'') and (cxDEdtComp_Date_End.Text<>'') then
begin
TmpQuery.Parameters.ParamByName('Comp_date_begin').Value:=cxDEdtComp_Date_Begin.Date;
TmpQuery.Parameters.ParamByName('Comp_date_end').Value:=cxDEdtComp_Date_End.Date;
end;
chilicsdn 2003-11-19
  • 打赏
  • 举报
回复
谢谢各位!继续讨论
那你试试直接写常量传入参数呢?
再试试用一个widestring保持字符串再传入参数
我直接些参数它也截

我发现我(圈套循环去掉) 把前面的'select * from Workflow_operation c where c.bill_num in ( 去掉后 他既然不截去
asj 2003-11-19
  • 打赏
  • 举报
回复
那你试试直接写常量传入参数呢?
再试试用一个widestring保持字符串再传入参数
tiexinliu 2003-11-19
  • 打赏
  • 举报
回复
我也遇见过,这样可以结决:
有中文出现的地方用字符串变量替换sql语句,不用参数传递。
--------------------------------------------------------------------
看尽悲伤,庸人自扰不平事。叹尽荒凉,海阔天空谁人知。狂风劲兮,百花飘
扬乱舞香。捏花一笑,海不扬波断肠心!
广告:http://delphicode.yeah.net
Erice 2003-11-19
  • 打赏
  • 举报
回复
在参数后补空格
chilicsdn 2003-11-19
  • 打赏
  • 举报
回复
我直接用字符串加了后还是可以了 就是不明白为什么会出现这种情况(应该是编码问题)
这是我改成字符串想加的函数 但是Bill_Class,Alarm_Class传中文都正常 呵呵
以后那位找到问题根源告诉我一声 先谢了!
ProceDure TForm1.BillStat(Deal_Man,Bill_Class,Alarm_Class,Start_Date_Begin,
Start_Date_End,Comp_Date_Begin,Comp_Date_End:string);
var
StrSql:string;
TmpQuery:TADOQuery;
Row,Col,Ret,i:integer;
begin
for i:=0 to StringGrid_StatResult.RowCount-1 do
begin
StringGrid_StatResult.Rows[i].Clear;
end;

Ret:=0;
if Trim(EdtDeal_Man.Text)<>'' then
begin
StringGrid_StatResult.ColCount:=3;
StringGrid_StatResult.Cells[0,0]:='业务类型';
StringGrid_StatResult.Cells[1,0]:='处理人';
StringGrid_StatResult.Cells[2,0]:='统计结果';
StrSql:='select deal_man,bill_type, Count(distinct a.bill_num) as Count from workflow_bill a,workflow_detail_bill b';
StrSql:=StrSql+' where a.bill_num=b.bill_num ';
StrSql:=StrSql+' and b.deal_man like :Deal_Man ';
StrSql:=StrSql+' and a.bill_class like :Bill_Class';
StrSql:=StrSql+' and a.alarm_class like :Alarm_Class';
if (Start_Date_Begin<>'') and (Start_Date_End<>'') then
begin
StrSql:=StrSql+' and a.start_date between :start_date_begin and :start_date_end';
end;
if (Comp_Date_Begin<>'') and (Comp_Date_End<>'') then
begin
StrSql:=StrSql+' and a.Actual_Comp_date between :Comp_date_begin and :Comp_date_end';
end;
StrSql:=StrSql+' group by deal_man,bill_type order by Deal_man';
end
else
begin
StringGrid_StatResult.ColCount:=2;
StringGrid_StatResult.Cells[0,0]:='业务类型';
StringGrid_StatResult.Cells[1,0]:='统计结果';
StrSql:='select bill_type, Count(distinct a.bill_num) as Count from workflow_bill a,workflow_detail_bill b';
StrSql:=StrSql+' where a.bill_num=b.bill_num ';
StrSql:=StrSql+' and b.deal_man like :Deal_Man ';
StrSql:=StrSql+' and a.bill_class like :Bill_Class';
StrSql:=StrSql+' and a.alarm_class like :Alarm_Class';
if (Start_Date_Begin<>'') and (Start_Date_End<>'') then
begin
StrSql:=StrSql+' and a.start_date between :start_date_begin and :start_date_end';
end;
if (Comp_Date_Begin<>'') and (Comp_Date_End<>'') then
begin
StrSql:=StrSql+' and a.Actual_Comp_date between :Comp_date_begin and :Comp_date_end';
end;
StrSql:=StrSql+' group by bill_type order by bill_type';
end;
try
TmpQuery:=TADOQuery.Create(self);
TmpQuery.Connection:=ADOConnection1;
TmpQuery.Close;
TmpQuery.SQL.Clear;
TmpQuery.SQL.Add(StrSql);

if Trim(EdtDeal_Man.Text)<>'' then
TmpQuery.Parameters.ParamByName('Deal_Man').Value:=Deal_Man
else
TmpQuery.Parameters.ParamByName('Deal_Man').Value:='%%';

if Trim(Bill_Class)<>'' then
TmpQuery.Parameters.ParamByName('Bill_Class').Value:=Trim(Bill_Class)
else
TmpQuery.Parameters.ParamByName('Bill_Class').Value:='%%';
if Trim(Alarm_Class)<>'' then
TmpQuery.Parameters.ParamByName('Alarm_Class').Value:=Trim(Alarm_Class)
else
TmpQuery.Parameters.ParamByName('Alarm_Class').Value:='%%';
if (Start_Date_Begin<>'') and (Start_Date_End<>'') then
begin
TmpQuery.Parameters.ParamByName('start_date_begin').Value:=StrToDate(Start_Date_Begin);
TmpQuery.Parameters.ParamByName('start_date_end').Value:=StrToDate(Start_Date_End);
end;

if (Comp_Date_Begin<>'') and (Comp_Date_End<>'') then
begin
TmpQuery.Parameters.ParamByName('Comp_date_begin').Value:=StrToDate(Comp_Date_Begin);
TmpQuery.Parameters.ParamByName('Comp_date_end').Value:=StrToDate(Comp_Date_End);
end;

TmpQuery.Prepared:=True;
TmpQuery.Open;
StringGrid_StatResult.RowCount:=TmpQuery.RecordCount+2;
Row:=1;
Col:=0;
while not TmpQuery.Eof do
begin
StringGrid_StatResult.Cells[0,Row]:=TmpQuery.FieldbyName('Bill_Type').AsString;
if Trim(Deal_Man)<>'' then
begin
StringGrid_StatResult.Cells[1,Row]:=TmpQuery.FieldbyName('Deal_man').AsString;
StringGrid_StatResult.Cells[2,Row]:=TmpQuery.FieldbyName('Count').AsString;
end
else
begin
StringGrid_StatResult.Cells[1,Row]:=TmpQuery.FieldbyName('Count').AsString;
end;
Ret:=Ret+ TmpQuery.FieldbyName('Count').AsInteger;
TmpQuery.Next;
Inc(Row);
end;
Finally
TmpQuery.Close;
TmpQuery.Free;
end;

StringGrid_StatResult.Cells[0,Row]:='累计';
if Trim(Deal_Man)<>'' then
begin
StringGrid_StatResult.Cells[2,Row]:=IntToStr(Ret);
end
else
begin
StringGrid_StatResult.Cells[1,Row]:=IntToStr(Ret);
end;
end;

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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