环境 DELPHI 5 + 所有已经出了的补丁
连接 ACCESS 数据库, 在做数据库插入操作的时候, 使用了参数(Parameters), ADOQuery1.SQL.Text:= 'Insert into tablename (description, code) values (:description, :code)';
ADOQuery1.Parameters.ParameterByName('description').Value = whateverDescritionis;
ADOQuery1.Parameters.ParameterByName('code').Value = somedelphicode;
ADOQuery1.ExecSQL;Description和Code两个字段都是MEMO类型.整个程序执行过程没有问题,但是结果是大大的有问题. code字段里面是一些C语言或者DELPHI语言的代码(长度有时会超过500K), 只要代码里面的所有单引号全部被重复了.也就是说, 如果CODE字段的内容是 String a = 'Hello world';
用上面方式插入后读出来以后是 String a=''Hello world'';我弄不清楚是什么原因, 怀疑是不是D5的ADO组件有缺陷或者是其他原因. 谁知道答案或者解决方案.
别告诉我使用动态的SQL, 那样问题更大.
连接 ACCESS 数据库, 在做数据库插入操作的时候, 使用了参数(Parameters), ADOQuery1.SQL.Text:= 'Insert into tablename (description, code) values (:description, :code)';
ADOQuery1.Parameters.ParameterByName('description').Value = whateverDescritionis;
ADOQuery1.Parameters.ParameterByName('code').Value = somedelphicode;
ADOQuery1.ExecSQL;Description和Code两个字段都是MEMO类型.整个程序执行过程没有问题,但是结果是大大的有问题. code字段里面是一些C语言或者DELPHI语言的代码(长度有时会超过500K), 只要代码里面的所有单引号全部被重复了.也就是说, 如果CODE字段的内容是 String a = 'Hello world';
用上面方式插入后读出来以后是 String a=''Hello world'';我弄不清楚是什么原因, 怀疑是不是D5的ADO组件有缺陷或者是其他原因. 谁知道答案或者解决方案.
别告诉我使用动态的SQL, 那样问题更大.
' 在sql写入时需要转换为其他符号(比如'')
而且我知道硬替换会留下无穷的问题,且效率低下. 我给个例子吧.在上面我提到的数据库中的CODE字段插入如下内容
Console.WriteLine("Please give me a number:");不替换任何内容的情况下, 用
ADOQuery1.SQL.text := 'Insert into table (description, code) values (''test'', ''Console.WriteLine("Please give me a number:");'')';
插入数据,系统报错, Parameter object is improperly defined. Inconsistent or incomplete information was provided如果插入内容由
Console.WriteLine("Please give me a number:");
改为
Console.WriteLine(Please give me a number:");
则没有任何问题. 请问,我应该如何替换字符? 但是如果使用参数(Parameters)进行数据插入则没有任何问题, 也不用做什么字符替换. 因为ADO组件会自己做这些事情的.
好像还可以把paramscheck的属性改成false但这时可能不能用绑定参数的形式了
var
memStrm: TMemoryStream;
begin
memStrm := TMemoryStream.Create;
try
ADOQuery1.SQL.Text:= 'Insert into tablename (description, code) values (:description, :code)';
memStrm.WriteBuffer(PChar(whateverDescritionis), Length(whateverDescritionis));
memStrm.Seek(0,soFromBeginning);
ADOQuery1.Parameters.ParameterByName('description').LoadFromStream(memStrm, ftBlob);
memStrm.Clear;
memStrm.WriteBuffer(PChar(somedelphicode), Length(somedelphicode));
memStrm.Seek(0,soFromBeginning);
ADOQuery1.Parameters.ParameterByName('code').LoadFromStream(memStrm, ftBlob);
ADOQuery1.ExecSQL;
finally
memStrm.Free;
end;
end;
中的ftBlob改为ftMemo. 否则插入数据库的东西为乱码.而且我相信这样也是最有效率的. 做字符替换是靠不住的.而且效率奇低.
这就给您加分.