这是我发数据的方法,发String 250长度以内的可以,怎样把ACCESS中备注格式的发过去?
type
MyRecord = Packed Record
Myid : Integer;
Mysn: String[250];
Myifshare : Boolean;
myshuyu:string[250];
mycontent:String;
mysubject:string[250]; end;
with MySendingRecord do
begin
mycontent := DataModule1.ADOQuery1.fieldbyname('content').Value ;
mysubject:= DataModule1.ADOQuery1.fieldbyname('subject').asstring;
myshuyu:= DataModule1.ADOQuery1.fieldbyname('shuyu').asstring;
Myid := DataModule1.ADOQuery1.fieldbyname('id').Value ;
Mysn := sn;
Myifshare := ifshare;
end;
// send and receive
with IdTCPClient1 do
begin
Host :='219.232.245.198';
Port :=9099;
Connect;
WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),true);
ReadBuffer(MyReceivingRecord,SizeOf(MyReceivingRecord));
Disconnect;
end;
end;
type
MyRecord = Packed Record
Myid : Integer;
Mysn: String[250];
Myifshare : Boolean;
myshuyu:string[250];
mycontent:String;
mysubject:string[250]; end;
with MySendingRecord do
begin
mycontent := DataModule1.ADOQuery1.fieldbyname('content').Value ;
mysubject:= DataModule1.ADOQuery1.fieldbyname('subject').asstring;
myshuyu:= DataModule1.ADOQuery1.fieldbyname('shuyu').asstring;
Myid := DataModule1.ADOQuery1.fieldbyname('id').Value ;
Mysn := sn;
Myifshare := ifshare;
end;
// send and receive
with IdTCPClient1 do
begin
Host :='219.232.245.198';
Port :=9099;
Connect;
WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),true);
ReadBuffer(MyReceivingRecord,SizeOf(MyReceivingRecord));
Disconnect;
end;
end;
MyRecord = Packed Record
Myid : Integer;
Mysn: String[250];
Myifshare : Boolean;
myshuyu:string[250];
mycontent:String;
mysubject:array[0..1024] of char;end; rec:myrecord;
strpcopy(@rec.mysubject,'');取的时候
strpas(strpas(@rec.mysubject));
是不是再加个0也行?有一些小说特别长
不让取不定长的,发的时候让发
mycontent:array of char;下面过不去:
strpas(Myrec.mycontent)
发送端 KK为string类型
client.WriteInteger(Length(kk));
client.WriteBuffer(kk[1],Length(kk));
服务器接收端 temp为string类型
TheSize :=AThread.Connection.ReadInteger;
SetLength(temp, TheSize);
AThread.Connection.ReadBuffer(temp[1],TheSize);
type
MyRecord = Packed Record
Myid : Integer;
Mysn: String[250];
Myifshare : Boolean;
myshuyu:string[250];
mycontentlen:integer;
mysubject:string[250];
end;procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
MyRec : MyRecord; temp:string;
beginif AThread.Connection.ReadLn='senddata' then
beginAThread.connection.ReadBuffer(MyRec,SizeOf(MyRec));if MyRec.Myifshare then
begin
memo1 .Text :=memo1 .Text+MyRec.Mysn+' 共享了 '+MyRec.mysubject+#13#10;
SetLength(temp, MyRec.mycontentlen);
AThread.Connection.ReadBuffer(temp[1],MyRec.mycontentlen);
with DataModule1.ADOQuery1 do
begin
Close;
sql.clear;
sql.add('insert into sub_table (sn,id,subject,content,shuyu) values ('''+(MyRec.mysn)+''','+inttostr(MyRec.myid)+','''+MyRec.mysubject+''',:content,'+(myrec.myshuyu)+' )');
Parameters.ParamByName('content').value:=temp;
ExecSQL;
end;
end
else
begin
Executesql('delete from sub_table where sn='''+(MyRec.mysn)+''' and id not in (select id from sub_table where shuyu=0 or shuyu in (select id from sub_table where sn='''+(MyRec.mysn)+''' ) ) ');
Executesql('delete from sub_table where sn='''+(MyRec.mysn)+''' and id='+inttostr(MyRec.myid)+' ');
memo1 .Text :=memo1 .Text+MyRec.Mysn+' 删除了 '+MyRec.mysubject+#13#10;end;
SendMessage(Memo1.Handle, EM_SCROLL, SB_BOTTOM, 0);
//AThread.connection.WriteBuffer(MyRec,SizeOf(MyRec),true);
end;end;
AThread.Connection.ReadBuffer(temp[1],MyRec.mycontentlen);
你这两句为何要这么处理啊 最好是这样 前面发了一个结构以后 再发一次下一个string的 length 然后在
readbuffer 记住 动态创建adoquery的时候 要记得创建前 coinitialize(nil); free后 couninitialize;
而且 引入activex
那么你应该这么做 把你的结构数据变成数据1^数据2^数据3^数据4^.... 变成一个大的string 这个数据可以不用规范多大 只是发的时候用我的方法法一下大小在发流即可 收到数据后 在把string拆开 根据^号的分割逐一取出数据给不同的变量
这样你完全可以一次发送完毕 而不用先发个数据结构在发一个大string 这样是不是简单多了
type
MyRecord = Packed Record
Myid : Integer;
Mysn: String[250];
Myifshare : Boolean;
myshuyu:string[250];
mycontent:String; //问题在这里,
mysubject:string[250];
end; 记录中不要用string类型.必须是指定长度的数据.比如用array[1..2048]of byte;
并且如果你的内容过长,你可以分多次发,自己定义一套可行的协议.+++++++++++++++++++++++++++++++++++++++++++++++++
你测试一下,看看这个记录的大小是否为5?:
TMyTestRecord = packed record
Name : string;
Age : Byte;
end;procedure TFormTransTest.btnTestSizeClick(Sender: TObject);
var
tstRcd : TMyTestRecord;
begin
tstRcd.Name := '我们祖上曾经辉煌过';
tstRcd.Age := 35;
ShowMessage(IntToStr(SizeOf(tstRcd ) ));
end;
var
myadoquery:Tadoquery;
temp,str:string;
myid,mysn,Myifshare,myshuyu,mycontentlen,mysubject:string;
sl:TStringList;
begin
if AThread.Connection.ReadLn='senddata' then
begin
str:=AThread.connection.Readln;
sl:=split(str,'^');if sl[2]='-1' then
begin
SetLength(temp, strtoint(sl[4]));
AThread.Connection.ReadBuffer(temp[1],strtoint(sl[4]));
try
coinitialize(nil);
myadoquery:=TADOQuery.Create(Application);
myadoquery.Connection:=DataModule1.ADOConnection1;
with myadoquery do
begin
Close;
sql.clear;
sql.add('insert into sub_table (sn,id,subject,content,shuyu) values ('''+sl[1]+''','+sl[0]+','''+sl[5]+''',:content,'+sl[3]+' )');
Parameters.ParamByName('content').value:=temp;
ExecSQL;
memo1 .Text :=memo1 .Text+sl[1]+' 共享了 '+sl[5]+#13#10;
end;
finally
couninitialize;
myadoquery.Free;
end;
end
else
begin
try
coinitialize(nil);
Executesql('delete from sub_table where sn='''+sl[1]+''' and id not in (select id from sub_table where shuyu=0 or shuyu in (select id from sub_table where sn='''+sl[1]+''' ) ) ');
Executesql('delete from sub_table where sn='''+sl[1]+''' and id='+sl[0]+' ');
memo1 .Text :=memo1 .Text+sl[1]+' 删除了 '+sl[5]+#13#10;
finally
couninitialize;
end;
end;
SendMessage(Memo1.Handle, EM_SCROLL, SB_BOTTOM, 0);
//AThread.connection.WriteBuffer(MyRec,SizeOf(MyRec),true);
end;end;
1,长的CONTENT不行
2,短的有时候突然说内存错误,感觉是不是哪有问题呀
var
myadoquery:Tadoquery;
temp,,tempstr,str:string;
TheSize:integer;
myid,mysn,Myifshare,myshuyu,mycontentlen,mysubject:string;
sl:TStringList;
begin
TheSize :=AThread.Connection.ReadInteger;
SetLength(temp, TheSize);
AThread.Connection.ReadBuffer(temp[1],TheSize);
myid:=copy(temp,0,pos('^',temp)-1); 从字符串找出第一个^号左边的数据取出
tempstr:=copy(str,pos('^',temp)+1,length(temp)-pos('^',temp)); 从字符串找出第一个^号右边数据取出
temp:=tempstr;重新赋值
mysn:=copy(temp,0,pos('^',temp)-1); 在获取第一个^号左边数据 往下以此类推
tempstr:=copy(str,pos('^',temp)+1,length(temp)-pos('^',temp));
temp:=tempstr;
Myifshare:=copy(temp,0,pos('^',temp)-1);
tempstr:=copy(str,pos('^',temp)+1,length(temp)-pos('^',temp));
temp:=tempstr;
......我就不写完了 一次类推 把所有的数据取出 要注意最后一组时候 也就是拆开只剩下 ***^@@@的时候 就要直接这样
变量a:=copy(temp,0,pos('^',temp)-1);
变量B:=copy(str,pos('^',temp)+1,length(temp)-pos('^',temp));
try
coinitialize(nil);
myadoquery:=TADOQuery.Create(Application);
myadoquery.Connection:=DataModule1.ADOConnection1;
with myadoquery do
begin
Close;
sql.clear;
sql.add('insert into sub_table (sn,id,subject,content,shuyu) values ('''+sl[1]+''','+sl[0]+','''+sl[5]+''',:content,'+sl[3]+' )');
Parameters.ParamByName('content').value:=temp;
ExecSQL;
memo1 .Text :=memo1 .Text+sl[1]+' 共享了 '+sl[5]+#13#10;
end;
myadoquery.Free; //注销内存要放这里
finally
couninitialize; end;
end
else
begin
try
//coinitialize(nil); 这里错了 只有创建query控件的时候 要用这句 这里这句不应该要
Executesql('delete from sub_table where sn='''+sl[1]+''' and id not in (select id from sub_table where shuyu=0 or shuyu in (select id from sub_table where sn='''+sl[1]+''' ) ) ');
Executesql('delete from sub_table where sn='''+sl[1]+''' and id='+sl[0]+' ');
memo1 .Text :=memo1 .Text+sl[1]+' 删除了 '+sl[5]+#13#10; 这里要改成 memo1.lines.add(内容);
这里要加上 myadoquery.free;
finally
couninitialize; //这句可以留着
end;
end;
SendMessage(Memo1.Handle, EM_SCROLL, SB_BOTTOM, 0); //这句完全没必要
//AThread.connection.WriteBuffer(MyRec,SizeOf(MyRec),true);
end; end;
你那边发送时候要这么构造 数据1+'^'+数据2..........这样 构造好后 在按我那样发送
你这样还搞不定的话 就要改行了
把 myadoquery.free;
放在
couninitialize;前面
这里在另一个地方也创建了query控件
是不是都要加?
var my_query:Tadoquery;
begin
my_query:=Tadoquery.Create(Application);
try
with my_query do
begin
Connection:=DataModule1.ADOConnection1;
Close;
sql.clear;
sql.add(sqlstr);
sql.SaveToFile('1.txt');
ExecSQL;
end;
finally
my_query.Free;
end;
end;
这里是不是也要
coinitialize(nil);
couninitialize;
?
coinitialize(nil);
couninitialize;
? 这两句是什么意思呀