delphi中如何将listbox中数据存入数据库

wslf111 2015-02-12 03:09:35
delphi中如何通过点击确认出库将listbox中数据存入数据库,如图所示,依次是模号=mh,数量=sl,单位=dw,仓库=ck。
...全文
328 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhoo163 2015-02-13
  • 打赏
  • 举报
回复
var i,k:Integer;
    mh,sl,dw,ck,SQL,ID,ID1:string;
begin
  k:=100;
  DateTimetoString(ID1,'yymmddhhnnsszzz',now);
  for i:=0 to ListBox1.Items.Count-1 do
  begin
    k:=k+1;
    ID:=ID1+inttoStr(k);
    S1:=ListBox1.Items[i];
    S2:=ListBox2.Items[i];
    S3:=ListBox3.Items[i];
    S4:=ListBox4.Items[i];
    SQL:='INSERT INTO zhongjian(mh,sl,dw,ck,ID) VALUES('+
       #39+S1+#39+','+
       #39+S2+#39+','+
       #39+S3+#39+','+
       #39+S4+#39+','+
       #39+ID+#39+')';
    adoquery1.sql.text:=SQL;
    adoquery1.execsql;
  end;
end;
上述代码仅代参考: 表字段中就有主键ID,ADOQuery连接数据库的方式,不一样,代码有异。
yct0605 2015-02-12
  • 打赏
  • 举报
回复
更正一下: with adoquery1 do begin close; sql.text:='insert into table1 values(mh,sl,dw,ck)'; execsql; end;
yct0605 2015-02-12
  • 打赏
  • 举报
回复

    //保存到数据库
  with adoquery1 do
   begin
     close;
     sql.text:='insert into table1 values(mh,sl,dw,ck)';
     sql.execsql;
   end;
wslf111 2015-02-12
  • 打赏
  • 举报
回复
引用 3 楼 yct0605 的回复:
上面有点问题,从新发一个

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  mh,sl,dw,ck:string;
begin
  for i:=0 to ListBox1.Items.Count-1 do
  begin
    mh:=ListBox1.Items[i];
    sl:=ListBox2.Items[i];
    dw:=ListBox3.Items[i];
    ck:=ListBox4.Items[i];
    //保存到数据库 inset into zhongjian values(mh,sl,dw,ck);
  end;
end;
我用的是adoquery怎么来写的?
wslf111 2015-02-12
  • 打赏
  • 举报
回复
引用 3 楼 yct0605 的回复:
上面有点问题,从新发一个

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  mh,sl,dw,ck:string;
begin
  for i:=0 to ListBox1.Items.Count-1 do
  begin
    mh:=ListBox1.Items[i];
    sl:=ListBox2.Items[i];
    dw:=ListBox3.Items[i];
    ck:=ListBox4.Items[i];
    //保存到数据库 inset into zhongjian values(mh,sl,dw,ck);
  end;
end;
引用 3 楼 yct0605 的回复:
上面有点问题,从新发一个

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  mh,sl,dw,ck:string;
begin
  for i:=0 to ListBox1.Items.Count-1 do
  begin
    mh:=ListBox1.Items[i];
    sl:=ListBox2.Items[i];
    dw:=ListBox3.Items[i];
    ck:=ListBox4.Items[i];
    //保存到数据库 inset into zhongjian values(mh,sl,dw,ck);
  end;
end;
yct0605 2015-02-12
  • 打赏
  • 举报
回复
上面有点问题,从新发一个

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  mh,sl,dw,ck:string;
begin
  for i:=0 to ListBox1.Items.Count-1 do
  begin
    mh:=ListBox1.Items[i];
    sl:=ListBox2.Items[i];
    dw:=ListBox3.Items[i];
    ck:=ListBox4.Items[i];
    //保存到数据库 inset into zhongjian values(mh,sl,dw,ck);
  end;
end;
yct0605 2015-02-12
  • 打赏
  • 举报
回复

procedure TForm1.ListBox1Click(Sender: TObject);
var
  i:Integer;
begin
  for i:=0 to ListBox1.Items.Count-1 do
    ShowMessage(ListBox1.Items[ListBox1.ItemIndex]);{循环取出每条数据,保存到数据库中}
end;
luxuewei5214 2015-02-12
  • 打赏
  • 举报
回复
用个for循环,一行行的执行sql语句,或者写好存储过程传参数都行

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧