我的界面1是一個button控件.界面2會么都沒有但是有一個adoconnection1和adoquery
datasource1,dbgrid1,button1.我運行程序后一切如常不會出錯但是我用界面1的button1來打開界面2.再用界面2的button1來關閉界面2.然后再用界面1的button1來打開界面2,問題就來了.出錯信息是這樣的:
message'access violation at address 00000000.read of address 00000000'代碼如下
Form1:procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Show;
end;form2:
procedure TForm2.FormShow(Sender: TObject);
begin
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=car;Data Source=sport_chen;Password=123';
adoconnection1.LoginPrompt:=false;
adoconnection1.Connected:=true;
adoquery1.Connection:=adoconnection1;
adoquery1.SQL.Add('select * from car');
adoquery1.Active:=true;
end;請幫忙!!!!!!!!!!!!!!!!1
datasource1,dbgrid1,button1.我運行程序后一切如常不會出錯但是我用界面1的button1來打開界面2.再用界面2的button1來關閉界面2.然后再用界面1的button1來打開界面2,問題就來了.出錯信息是這樣的:
message'access violation at address 00000000.read of address 00000000'代碼如下
Form1:procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Show;
end;form2:
procedure TForm2.FormShow(Sender: TObject);
begin
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=car;Data Source=sport_chen;Password=123';
adoconnection1.LoginPrompt:=false;
adoconnection1.Connected:=true;
adoquery1.Connection:=adoconnection1;
adoquery1.SQL.Add('select * from car');
adoquery1.Active:=true;
end;請幫忙!!!!!!!!!!!!!!!!1
Form2.Free;
你看一下我的代碼不就知道了.
form2:
procedure TForm2.Formclose(Sender: TObject);
begin
action:=cafree;
form2:=nil;
(假定form2为你的界面2的名字)
就是adoconnection1連接到sqlserver數據庫后在form2關閉后沒有釋放.
沒有完全關掉程序時再打開form2就又一次把adoconnection1鏈接到sqlserver中.
但是我要怎樣才能在關閉form2時順便關掉adoconnection1的鏈接呢?
所以沒有用.出錯信息一樣.
if form2=nil then
form2:=TForm2.create(application);
form2.show;(或是form2.showmodal)
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=car;Data Source=sport_chen;Password=123';
注意上面一句语法对吗?
我忘了我用這個更好!
form1的button1的click事件寫
application.createform(tform2,form2);
form2.show;
這樣就不會出錯了.
我會給你分的請放心.
我上面的代碼沒有錯啊!