Delphi数据库连接使用访问和ADO连接

时间:2022-09-27 11:58:53

Okay so basically I've been working on my computing project for a while now and I've got 90% of it working however I'm having a problem with Delphi where is says that my database is not connected/ there is a problem connecting however I've already tried writing the information to the screen and this showed me that the items I was looking to pick up where in fact being picked up so the failure is when the items are being input in to the database. This however shouldn't be happening as the System already has database information displayed from that table and the user can physically select things from the database tables within the program however when trying to store the information back into the database it just breaks. Me and my computing teacher can not work it out, any help would be appreciated.

好的所以我一直在做我的计算项目一段时间现在,我有90%的工作但是我有一个问题,德尔福在哪里说我的数据库没有连接/连接但是有一个问题我已经尝试写在屏幕上的信息,这给我的项目我想接接事实上失败是当项目被输入到数据库中。但是这不应该发生,因为系统已经从该表中显示了数据库信息,用户可以从程序中的数据库表中物理地选择内容,但是当试图将信息存储回数据库时,它就会中断。我和我的计算机老师不能解决它,任何帮助都将感激。

The problem appears on the new orders page. If you'd rather look at the system then you can download it from here https://drive.google.com/folderview?id=0B_iRfwwM9QpHVXJnSkx4U1FjMlk&usp=sharing

问题出现在新订单页面。如果您愿意查看系统,那么您可以从这里下载它:https://drive.google.com/folderview?

procedure Tform1.btnSaveClick(Sender: TObject);
var orderID:integer;
count:integer;
begin
  try
    //save into the order table first
    tblOrder.Open;
    tblOrder.Insert;
    tblOrder.FieldByName('CustomerID').value:= strtoint(cboCustomer.Text);
tblOrder.Close;
tblOrder.Open;
tblOrder.Last;
orderID:=tblOrder.FieldByName('OrderID').Value;
showmessage(inttostr(orderID));


for count := 1 to nextFree-1 do
 begin
    if itemOrdered[count,1]<>0 then
    begin
      tblOrderLine.Open;
      tblOrderLine.AppendRecord([orderID, itemOrdered[count,1],itemOrdered[count,2]]);
    end;
  end;
showmessage('The order has been saved');
  except
       showmessage('There was a problem connecting to the database');
  end;
end;

1 个解决方案

#1


2  

You're doing far too much open, do something, close, open. Don't do that, because it's almost certain that is the cause of your problem. If the data is already being displayed, the database is open already. If you want it to keep being displayed, the database has to remain open.

你做的太多了,做点什么,靠近点,打开。不要那样做,因为这几乎肯定是你的问题的原因。如果数据已经显示,则数据库已经打开。如果希望它继续显示,数据库必须保持打开状态。

I also removed your try..except. You can put it back in if you'd like; I personally like to allow the exception to occur so that I can find out why the database operation failed from the exception message, rather than hide it and have no clue what caused it not to work.

我也取消了你的尝试。如果你愿意,你可以把它放回去;我个人喜欢允许异常发生,这样我就可以从异常消息中找出为什么数据库操作失败,而不是隐藏它,并且不知道是什么原因导致它不能工作。

procedure Tform1.btnSaveClick(Sender: TObject);
var 
  orderID: integer;
  count: integer;
begin
  //save into the order table first
  tblOrder.Insert;
  tblOrder.FieldByName('CustomerID').value:= strtoint(cboCustomer.Text);
  tblOrder.Post;
  orderID:=tblOrder.FieldByName('OrderID').Value;
  showmessage(inttostr(orderID));

  for count := 1 to nextFree-1 do
  begin
    if itemOrdered[count, 1] <> 0 then
    begin
      tblOrderLine.AppendRecord([orderID, itemOrdered[count,1],itemOrdered[count,2]]);
      tblOrderLine.Post;
    end;
  end;
  showmessage('The order has been saved');
end;

#1


2  

You're doing far too much open, do something, close, open. Don't do that, because it's almost certain that is the cause of your problem. If the data is already being displayed, the database is open already. If you want it to keep being displayed, the database has to remain open.

你做的太多了,做点什么,靠近点,打开。不要那样做,因为这几乎肯定是你的问题的原因。如果数据已经显示,则数据库已经打开。如果希望它继续显示,数据库必须保持打开状态。

I also removed your try..except. You can put it back in if you'd like; I personally like to allow the exception to occur so that I can find out why the database operation failed from the exception message, rather than hide it and have no clue what caused it not to work.

我也取消了你的尝试。如果你愿意,你可以把它放回去;我个人喜欢允许异常发生,这样我就可以从异常消息中找出为什么数据库操作失败,而不是隐藏它,并且不知道是什么原因导致它不能工作。

procedure Tform1.btnSaveClick(Sender: TObject);
var 
  orderID: integer;
  count: integer;
begin
  //save into the order table first
  tblOrder.Insert;
  tblOrder.FieldByName('CustomerID').value:= strtoint(cboCustomer.Text);
  tblOrder.Post;
  orderID:=tblOrder.FieldByName('OrderID').Value;
  showmessage(inttostr(orderID));

  for count := 1 to nextFree-1 do
  begin
    if itemOrdered[count, 1] <> 0 then
    begin
      tblOrderLine.AppendRecord([orderID, itemOrdered[count,1],itemOrdered[count,2]]);
      tblOrderLine.Post;
    end;
  end;
  showmessage('The order has been saved');
end;