首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么让EXCEL导入SQL的数据不能有重复

2013-07-01 
如何让EXCEL导入SQL的数据不能有重复?数据库中的表将多个字段一块设为主键,如何实现将数据从Excel中导入到

如何让EXCEL导入SQL的数据不能有重复?
数据库中的表将多个字段一块设为主键,如何实现将数据从Excel中导入到SQL中,不能有重复

[解决办法]

procedure TTEORDFO.dxBarLargeButton1Click(Sender: TObject);
//引用
uses ComObj
var
    ASql, APath: String;
    ExcelApp: Variant;
    AInt: Integer;
begin
  with ADOQuery2 do
    begin
        Close;
        sql.Clear;
        SQL.Add('select MAX(SDREN) as SDREN from TEORDL');
        Open;
        if FieldByName('SDREN').AsString<>'' then
        begin
         MessageBox(Self.Handle,'你数据库有未完成的导入的数据,请等待之前的数据完成在导入!','系统提示',mb_iconquestion);
        end
        else
        begin

                  if messagebox(Self.Handle,'你确定要导入联想TMS数据导入?','系统提示',mb_yesno+mb_iconquestion)=idyes then
                      begin
                          If OpenDialog1.Execute Then
                            APath := OpenDialog1.FileName
                          Else
                            Exit;

                          If not FileExists(APath) Then
                            Begin
                              messagebox(self.Handle,'你选择的文件不存,请核对','提示信息',mb_iconinformation);
                              Exit;
                            End;
                          Try
                            Application.ProcessMessages;


                            ExcelApp := CreateOleObject('Excel.Application');
                            ExcelApp.Workbooks.Open(APath);
                            ExcelApp.WorkSheets[1].Activate;

                            ASql := 'select * from TEORDL where 1=2';     //表名 ,也可以在show里面先打开数据集
                             ADOQuery1.Close;       //数据集的 LockType属性 要设为 ltBatchOptimistic
                             ADOQuery1.SQL.Text := ASql;    //才可以批量保存
                             ADOQuery1.Open;


                            For AInt := 2 To ExcelApp.ActiveSheet.UsedRange.rows.count Do //AInt 起始行数,重哪行开始导入,Aint初始值就是几
                              Begin
                                     ADOQuery1.Append;
                                     ADOQuery1.FieldByName('SDDATE').AsDateTime := ExcelApp.Cells[AInt, 1].Value;   //Aint是第几行,后面的数字是第几列,
                                     ADOQuery1.FieldByName('SDREN').AsString := ExcelApp.Cells[AInt, 2].Value;
                                     ADOQuery1.FieldByName('KHNAME').AsString := ExcelApp.Cells[AInt, 3].Value;
                                     ADOQuery1.FieldByName('KHADDESS').AsString := ExcelApp.Cells[AInt, 4].Value;
                                     ADOQuery1.FieldByName('KHTEll').AsString := ExcelApp.Cells[AInt, 5].Value;
                                     ADOQuery1.FieldByName('KHTOPO').AsString := ExcelApp.Cells[AInt, 6].Value;


                                     ADOQuery1.FieldByName('SKUNAME').AsString := ExcelApp.Cells[AInt, 7].Value;
                                     ADOQuery1.FieldByName('SKU').AsString := ExcelApp.Cells[AInt, 8].Value;
                                     ADOQuery1.FieldByName('CQTY').AsFloat := ExcelApp.Cells[AInt, 9].Value;
                                     ADOQuery1.FieldByName('ZQTY').AsFloat := ExcelApp.Cells[AInt, 10].Value;
                                     ADOQuery1.FieldByName('JQTY').AsFloat := ExcelApp.Cells[AInt, 11].Value;
                                     ADOQuery1.Post;
                                   End;
                             Finally
                               ExcelApp.WorkBooks.Close;
                               ExcelApp.quit;
                             End;
                      end;

        end;
    end;
end;




热点排行