我用delphi做了一個輸入畫面我是用dbgrid瀏覽數據庫的(access)我的保存按鈕代碼如下:
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('insert into xj(djh,hjkm,bmb,csb,sr,zc,tim,zy)values(:djh,:hjkm,:bmb,:csb,:sr,:zc,:tim,:zy)');
adoquery1.Parameters.ParamByName('djh').Value:=edit1.Text;
adoquery1.Parameters.ParamByName('hjkm').Value:=combobox1.Text;
adoquery1.Parameters.ParamByName('bmb').Value:= combobox2.Text;
adoquery1.Parameters.ParamByName('csb').Value:= combobox3.Text;
adoquery1.Parameters.ParamByName('sr').Value:= edit2.Text;
adoquery1.Parameters.ParamByName('zc').Value:= edit3.Text;
adoquery1.Parameters.ParamByName('tim').Value:= datetimepicker1.DateTime;
adoquery1.Parameters.ParamByName('zy').Value:= edit4.Text;
adoquery1.ExecSQL;
為何我一按保存按鈕dbgrid的數據都不見了.后來我在代碼后加上
adoquery1.Close;
adoquery1.Active:=false;
adoquery1.Active:=true;
執行時報錯.請問大哥是什麼原因啊!
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('insert into xj(djh,hjkm,bmb,csb,sr,zc,tim,zy)values(:djh,:hjkm,:bmb,:csb,:sr,:zc,:tim,:zy)');
adoquery1.Parameters.ParamByName('djh').Value:=edit1.Text;
adoquery1.Parameters.ParamByName('hjkm').Value:=combobox1.Text;
adoquery1.Parameters.ParamByName('bmb').Value:= combobox2.Text;
adoquery1.Parameters.ParamByName('csb').Value:= combobox3.Text;
adoquery1.Parameters.ParamByName('sr').Value:= edit2.Text;
adoquery1.Parameters.ParamByName('zc').Value:= edit3.Text;
adoquery1.Parameters.ParamByName('tim').Value:= datetimepicker1.DateTime;
adoquery1.Parameters.ParamByName('zy').Value:= edit4.Text;
adoquery1.ExecSQL;
為何我一按保存按鈕dbgrid的數據都不見了.后來我在代碼后加上
adoquery1.Close;
adoquery1.Active:=false;
adoquery1.Active:=true;
執行時報錯.請問大哥是什麼原因啊!
所以當你改變AdoQuery1的時候,數據集就會發生變化
也就是說,當你執行
adoquery1.ExecSQL;
這條語句之後,AdoQuery1數據集變成了空,所以
沒有記錄adoquery1.Close;
adoquery1.Active:=false;
adoquery1.Active:=true;
會出錯是因為,你前面的
adoquery1.SQL.Add('insert into xj(djh,hjkm,bmb,csb,sr,zc,tim,zy)values(:djh,:hjkm,:bmb,:csb,:sr,:zc,:tim,:zy)');
改變了AdoQuery1.SQL.TEXT值,所以會出錯樓主其實可以用 with adoquery1 do
begin
Edit;
FieldByName('djh').Value:=edit1.Text;
FieldByName('hjkm').Value:=combobox1.Text;
FieldByName('bmb').Value:= combobox2.Text;
FieldByName('csb').Value:= combobox3.Text;
FieldByName('sr').Value:= edit2.Text;
FieldByName('zc').Value:= edit3.Text;
FieldByName('tim').Value:= datetimepicker1.DateTime;
FieldByName('zy').Value:= edit4.Text;
Post;
end;
沒有調試,我想應該是可以的