Delphi com Excel e SQL Server
Olá Galera, fiz meu Sql trabalhar com um Excel via Delphi (ADO)
Td certo... Até que me gerou esse erro...
´Key Column information is insufficient or incorrect, Too Many rows were affected by update´
O que seria isso? alguem pode me ajudar...
Abaixo segue o código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls, WinSkinData, ExtCtrls,
DBCtrls, ComCtrls, Buttons, DBGridZebraADO, ShellAPI;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
qryExcel: TADOQuery;
DataSource1: TDataSource;
qryEstoque: TADOQuery;
ADOConnection2: TADOConnection;
DataSource2: TDataSource;
qryEstoqueAgrup: TStringField;
qryEstoqueCdPrd: TStringField;
qryEstoqueDescCdPrd: TStringField;
qryEstoqueMedida: TStringField;
qryEstoqueSaldo: TFloatField;
Button1: TButton;
Button3: TButton;
Panel1: TPanel;
DBGrid1: TDBGrid;
Panel2: TPanel;
Label1: TLabel;
Label3: TLabel;
Panel3: TPanel;
Panel4: TPanel;
Label2: TLabel;
Label4: TLabel;
Button2: TButton;
Panel5: TPanel;
Label5: TLabel;
Label6: TLabel;
edtArq: TEdit;
Button4: TButton;
Label7: TLabel;
OpenDialog1: TOpenDialog;
Label8: TLabel;
edtServ: TComboBox;
Button7: TButton;
Label9: TLabel;
DBText1: TDBText;
Button8: TButton;
ProgressBar1: TProgressBar;
SpeedButton1: TSpeedButton;
Label10: TLabel;
Label11: TLabel;
DBGridZebraADO1: TDBGridZebraADO;
Button5: TButton;
qryExcelAGP: TWideStringField;
qryExcelCOD: TFloatField;
qryExcelDESCRIO: TWideStringField;
qryExcelMed: TWideStringField;
qryExcelSaldo: TFloatField;
qryExcelAtual: TFloatField;
qryExcelDiferena: TFloatField;
Button6: TButton;
qryExcelDif: TFloatField;
Label12: TLabel;
procedure qryEstoqueAfterOpen(DataSet: TDataSet);
procedure qryExcelAfterOpen(DataSet: TDataSet);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure qryExcelCalcFields(DataSet: TDataSet);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure edtArqDblClick(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure qryEstoqueAfterScroll(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
Texto1, Texto2 : String;
Stop : Boolean;
end;
var
Form1: TForm1;
mArq : String;
implementation
uses Detalhe, diferencas;
{$R *.dfm}
procedure TForm1.qryEstoqueAfterOpen(DataSet: TDataSet);
begin
Label2.Caption := INttostr(qryEstoque.RecordCount);
end;
procedure TForm1.qryExcelAfterOpen(DataSet: TDataSet);
begin
Label1.Caption := Inttostr(qryExcel.RecordCount);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryEstoque.IsEmpty Then exit;
if qryExcel.IsEmpty Then exit;
Stop := True;
qryEstoque.First;
qryExcel.First;
Panel5.Visible := True;
Panel5.Update;
qryEstoque.DisableControls;
qryExcel.DisableControls;
ProgressBar1.Position := 0;
frmResumo.Memo1.Clear;
frmResumo.Memo2.Clear;
Label11.Caption := ´ de ´ + IntToStr(qryEstoque.RecordCount);
ProgressBar1.Max := qryEstoque.RecordCount;
While not qryEstoque.Eof do
Begin
mCdPrd := qryEstoqueCdPrd.Value;
Label10.Caption := IntToStr(qryEstoque.RecNo);
Label10.Update;
if qryExcel.Locate(´COD´, mCdPrd, [loPartialKey]) then
Begin
qryExcel.Edit;
qryExcelAtual.Value := qryEstoqueSaldo.Value;
frmResumo.Memo1.Lines.Add(qryEstoqueCdPrd.Value + ´ - ´ + qryEstoqueDescCdPrd.Value);
end //LOCATE }
else
frmResumo.Memo2.Lines.Add(qryEstoqueCdPrd.Value + ´ - ´ + qryEstoqueDescCdPrd.Value);
ProgressBar1.Position := ProgressBar1.Position + 1;
if Stop = False Then Break;
Application.ProcessMessages;
qryEstoque.Next;
end; //WHILE
qryEstoque.EnableControls;
qryExcel.EnableControls;
qryEstoque.First;
qryExcel.First;
Panel5.Visible := False;
Panel5.Update;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryEstoque.IsEmpty then Exit;
mcdPrd := qryEstoqueCdPrd.Value;
if not qryExcel.Locate(´COD´, mCdPrd, [loPartialKey]) then
Showmessage(´Nao foi localizado´);
end;
procedure TForm1.qryExcelCalcFields(DataSet: TDataSet);
begin
qryExcelDif.Value := qryExcelSaldo.Value - qryExcelAtual.Value;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if qryExcelDif.Value <> 0 then
Begin
DBGrid1.Canvas.Font.Style :=
DBGrid1.Canvas.Font.Style + [fsBold];
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if Button4.Caption = ´Conectar´ then
Begin
ADOConnection1.Close;
ADOConnection2.Close;
if edtServ.ItemIndex = -1 then Exit;
if edtArq.Text = ´´ Then Exit;
ADOConnection1.ConnectionString :=
´Provider=Microsoft.Jet.OLEDB.4.0;´+
´Data Source=´+ edtarq.text +´;´+
´Extended Properties=Excel 8.0;´+
´Persist Security Info=False´;
ADOConnection2.ConnectionString :=
´Provider=SQLOLEDB.1;´+
´Persist Security Info=False;´+
´User ID=sa;´+
´Initial Catalog=monreve;´+
´Data Source=´+ edtServ.Text+´;´;
Button4.Caption := ´Desconectar´;
Try
ADOConnection1.Open;
ADOConnection2.Open;
qryExcel.Open;
qryEstoque.Open;
Caption := ´Conectado a :´ + edtArq.Text + ´ em ´Servidor Monreve´´;
except
Showmessage(´Ocorreu um Erro Não Esperado´);
Button4.Caption := ´Conectar´;
end;
End //If
else
Begin
ADOConnection1.Close;
ADOConnection2.Close;
Label1.Caption := ´´;
Label2.Caption := ´´;
Caption := ´Diferença de Estoque - Lucinei´;
Button4.Caption := ´Conectar´;
End;
end;
function LTrim(cString: String): String;
var
Count, i: Byte;
begin
Count := Length(cString);
if Count > 0 then begin
for i := 1 to Count do begin
if cString <> ´ ´ then Break;
end;
cString := Copy(cString,i,Count-i+1);
end;
LTrim := cString;
end;
function RTrim(cString: String): String;
var
Count, i: Byte;
begin
Count := Length(cString);
if Count > 0 then begin
for i := Count downto 1 do begin
if cString <> ´ ´ then Break;
end;
cString := Copy(cString,1,i);
end;
RTrim := cString;
end;
function LeftZero(cString: String; nPosicoes: Word): String;
Const
cDigitosValidos: PChar = ´ 0123456789/´;
Var
lDigitoNumerico: Boolean;
nString: Integer;
begin
cString := RTrim(LTrim(cString));
lDigitoNumerico := True;
nString := Length( cString );
while nString > 0 do begin
If StrScan(cDigitosValidos, cString[nString]) = nil then begin
lDigitoNumerico := False;
nString := 0
end
else
Dec(nString);
end;
If lDigitoNumerico = True then begin
while Length( cString ) < nPosicoes do begin
cString := ´0´+ cString;
end;
end;
LeftZero := cString;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryExcel.IsEmpty then Exit;
mCdPrd := LeftZero(FloatToStr(qryExcelCOD.value), 4);
if not qryEstoque.Locate(´CdPrd´, mCdPrd, [loPartialKey]) then
Showmessage(´Nao foi localizado´);
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
frmResumo.Label3.Caption := ´Comparado(s) ´ + IntToStr(frmResumo.Memo1.Lines.Count) + ´ registro(s)´ + ´ de ´ + IntToStr(qryExcel.RecordCount);
frmResumo.Label3.Update;
frmResumo.Label4.Caption := ´Não Comparado(s) ´ + IntToStr(frmResumo.Memo2.Lines.Count) + ´ registro(s) ´ + ´de ´ + IntToStr(qryEstoque.RecordCount);
frmResumo.Label4.Update;
frmResumo.ShowModal;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Stop := False;
end;
procedure TForm1.edtArqDblClick(Sender: TObject);
begin
if OpenDialog1.Execute then
edtArq.Text := OpenDialog1.FileName;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if Button4.Caption = ´Desconectar´ then
Begin
ShowMessage(´Por Favor, desconecte antes de abrir´);
Exit;
End;
if edtArq.Text = ´´ then exit;
ShellExecute(Handle, nil, PChar(edtArq.Text) , nil, nil, SW_SHOWNORMAL);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if MessageDlg(´Refazer Contagem?!´,mtWarning,[mbYes,mbNo],1) = mrYes then
begin
frmDif.Memo1.Clear;
qryExcel.First;
while not qryExcel.Eof do
Begin
if qryExcelDiferena.Value <> 0 then
frmDif.Memo1.Lines.Add(FloatToStr(qryExcelCOD.Value) + ´ - ´ + qryExcelDESCRIO.Value);
qryExcel.Next;
End; //While Not
frmDif.Caption := ´Diferença em : ´ + IntTOStr(frmDif.Memo1.Lines.Count) + ´ produtos´;
end; //If
frmDif.ShowModal;
end;
procedure TForm1.qryEstoqueAfterScroll(DataSet: TDataSet);
begin
Label12.Caption := IntToStr(qryEstoque.RecNo);
end;
end.
Td certo... Até que me gerou esse erro...
´Key Column information is insufficient or incorrect, Too Many rows were affected by update´
O que seria isso? alguem pode me ajudar...
Abaixo segue o código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls, WinSkinData, ExtCtrls,
DBCtrls, ComCtrls, Buttons, DBGridZebraADO, ShellAPI;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
qryExcel: TADOQuery;
DataSource1: TDataSource;
qryEstoque: TADOQuery;
ADOConnection2: TADOConnection;
DataSource2: TDataSource;
qryEstoqueAgrup: TStringField;
qryEstoqueCdPrd: TStringField;
qryEstoqueDescCdPrd: TStringField;
qryEstoqueMedida: TStringField;
qryEstoqueSaldo: TFloatField;
Button1: TButton;
Button3: TButton;
Panel1: TPanel;
DBGrid1: TDBGrid;
Panel2: TPanel;
Label1: TLabel;
Label3: TLabel;
Panel3: TPanel;
Panel4: TPanel;
Label2: TLabel;
Label4: TLabel;
Button2: TButton;
Panel5: TPanel;
Label5: TLabel;
Label6: TLabel;
edtArq: TEdit;
Button4: TButton;
Label7: TLabel;
OpenDialog1: TOpenDialog;
Label8: TLabel;
edtServ: TComboBox;
Button7: TButton;
Label9: TLabel;
DBText1: TDBText;
Button8: TButton;
ProgressBar1: TProgressBar;
SpeedButton1: TSpeedButton;
Label10: TLabel;
Label11: TLabel;
DBGridZebraADO1: TDBGridZebraADO;
Button5: TButton;
qryExcelAGP: TWideStringField;
qryExcelCOD: TFloatField;
qryExcelDESCRIO: TWideStringField;
qryExcelMed: TWideStringField;
qryExcelSaldo: TFloatField;
qryExcelAtual: TFloatField;
qryExcelDiferena: TFloatField;
Button6: TButton;
qryExcelDif: TFloatField;
Label12: TLabel;
procedure qryEstoqueAfterOpen(DataSet: TDataSet);
procedure qryExcelAfterOpen(DataSet: TDataSet);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure qryExcelCalcFields(DataSet: TDataSet);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure edtArqDblClick(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure qryEstoqueAfterScroll(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
Texto1, Texto2 : String;
Stop : Boolean;
end;
var
Form1: TForm1;
mArq : String;
implementation
uses Detalhe, diferencas;
{$R *.dfm}
procedure TForm1.qryEstoqueAfterOpen(DataSet: TDataSet);
begin
Label2.Caption := INttostr(qryEstoque.RecordCount);
end;
procedure TForm1.qryExcelAfterOpen(DataSet: TDataSet);
begin
Label1.Caption := Inttostr(qryExcel.RecordCount);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryEstoque.IsEmpty Then exit;
if qryExcel.IsEmpty Then exit;
Stop := True;
qryEstoque.First;
qryExcel.First;
Panel5.Visible := True;
Panel5.Update;
qryEstoque.DisableControls;
qryExcel.DisableControls;
ProgressBar1.Position := 0;
frmResumo.Memo1.Clear;
frmResumo.Memo2.Clear;
Label11.Caption := ´ de ´ + IntToStr(qryEstoque.RecordCount);
ProgressBar1.Max := qryEstoque.RecordCount;
While not qryEstoque.Eof do
Begin
mCdPrd := qryEstoqueCdPrd.Value;
Label10.Caption := IntToStr(qryEstoque.RecNo);
Label10.Update;
if qryExcel.Locate(´COD´, mCdPrd, [loPartialKey]) then
Begin
qryExcel.Edit;
qryExcelAtual.Value := qryEstoqueSaldo.Value;
frmResumo.Memo1.Lines.Add(qryEstoqueCdPrd.Value + ´ - ´ + qryEstoqueDescCdPrd.Value);
end //LOCATE }
else
frmResumo.Memo2.Lines.Add(qryEstoqueCdPrd.Value + ´ - ´ + qryEstoqueDescCdPrd.Value);
ProgressBar1.Position := ProgressBar1.Position + 1;
if Stop = False Then Break;
Application.ProcessMessages;
qryEstoque.Next;
end; //WHILE
qryEstoque.EnableControls;
qryExcel.EnableControls;
qryEstoque.First;
qryExcel.First;
Panel5.Visible := False;
Panel5.Update;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryEstoque.IsEmpty then Exit;
mcdPrd := qryEstoqueCdPrd.Value;
if not qryExcel.Locate(´COD´, mCdPrd, [loPartialKey]) then
Showmessage(´Nao foi localizado´);
end;
procedure TForm1.qryExcelCalcFields(DataSet: TDataSet);
begin
qryExcelDif.Value := qryExcelSaldo.Value - qryExcelAtual.Value;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if qryExcelDif.Value <> 0 then
Begin
DBGrid1.Canvas.Font.Style :=
DBGrid1.Canvas.Font.Style + [fsBold];
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if Button4.Caption = ´Conectar´ then
Begin
ADOConnection1.Close;
ADOConnection2.Close;
if edtServ.ItemIndex = -1 then Exit;
if edtArq.Text = ´´ Then Exit;
ADOConnection1.ConnectionString :=
´Provider=Microsoft.Jet.OLEDB.4.0;´+
´Data Source=´+ edtarq.text +´;´+
´Extended Properties=Excel 8.0;´+
´Persist Security Info=False´;
ADOConnection2.ConnectionString :=
´Provider=SQLOLEDB.1;´+
´Persist Security Info=False;´+
´User ID=sa;´+
´Initial Catalog=monreve;´+
´Data Source=´+ edtServ.Text+´;´;
Button4.Caption := ´Desconectar´;
Try
ADOConnection1.Open;
ADOConnection2.Open;
qryExcel.Open;
qryEstoque.Open;
Caption := ´Conectado a :´ + edtArq.Text + ´ em ´Servidor Monreve´´;
except
Showmessage(´Ocorreu um Erro Não Esperado´);
Button4.Caption := ´Conectar´;
end;
End //If
else
Begin
ADOConnection1.Close;
ADOConnection2.Close;
Label1.Caption := ´´;
Label2.Caption := ´´;
Caption := ´Diferença de Estoque - Lucinei´;
Button4.Caption := ´Conectar´;
End;
end;
function LTrim(cString: String): String;
var
Count, i: Byte;
begin
Count := Length(cString);
if Count > 0 then begin
for i := 1 to Count do begin
if cString <> ´ ´ then Break;
end;
cString := Copy(cString,i,Count-i+1);
end;
LTrim := cString;
end;
function RTrim(cString: String): String;
var
Count, i: Byte;
begin
Count := Length(cString);
if Count > 0 then begin
for i := Count downto 1 do begin
if cString <> ´ ´ then Break;
end;
cString := Copy(cString,1,i);
end;
RTrim := cString;
end;
function LeftZero(cString: String; nPosicoes: Word): String;
Const
cDigitosValidos: PChar = ´ 0123456789/´;
Var
lDigitoNumerico: Boolean;
nString: Integer;
begin
cString := RTrim(LTrim(cString));
lDigitoNumerico := True;
nString := Length( cString );
while nString > 0 do begin
If StrScan(cDigitosValidos, cString[nString]) = nil then begin
lDigitoNumerico := False;
nString := 0
end
else
Dec(nString);
end;
If lDigitoNumerico = True then begin
while Length( cString ) < nPosicoes do begin
cString := ´0´+ cString;
end;
end;
LeftZero := cString;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
mCdPrd : String;
begin
if qryExcel.IsEmpty then Exit;
mCdPrd := LeftZero(FloatToStr(qryExcelCOD.value), 4);
if not qryEstoque.Locate(´CdPrd´, mCdPrd, [loPartialKey]) then
Showmessage(´Nao foi localizado´);
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
frmResumo.Label3.Caption := ´Comparado(s) ´ + IntToStr(frmResumo.Memo1.Lines.Count) + ´ registro(s)´ + ´ de ´ + IntToStr(qryExcel.RecordCount);
frmResumo.Label3.Update;
frmResumo.Label4.Caption := ´Não Comparado(s) ´ + IntToStr(frmResumo.Memo2.Lines.Count) + ´ registro(s) ´ + ´de ´ + IntToStr(qryEstoque.RecordCount);
frmResumo.Label4.Update;
frmResumo.ShowModal;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Stop := False;
end;
procedure TForm1.edtArqDblClick(Sender: TObject);
begin
if OpenDialog1.Execute then
edtArq.Text := OpenDialog1.FileName;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if Button4.Caption = ´Desconectar´ then
Begin
ShowMessage(´Por Favor, desconecte antes de abrir´);
Exit;
End;
if edtArq.Text = ´´ then exit;
ShellExecute(Handle, nil, PChar(edtArq.Text) , nil, nil, SW_SHOWNORMAL);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if MessageDlg(´Refazer Contagem?!´,mtWarning,[mbYes,mbNo],1) = mrYes then
begin
frmDif.Memo1.Clear;
qryExcel.First;
while not qryExcel.Eof do
Begin
if qryExcelDiferena.Value <> 0 then
frmDif.Memo1.Lines.Add(FloatToStr(qryExcelCOD.Value) + ´ - ´ + qryExcelDESCRIO.Value);
qryExcel.Next;
End; //While Not
frmDif.Caption := ´Diferença em : ´ + IntTOStr(frmDif.Memo1.Lines.Count) + ´ produtos´;
end; //If
frmDif.ShowModal;
end;
procedure TForm1.qryEstoqueAfterScroll(DataSet: TDataSet);
begin
Label12.Caption := IntToStr(qryEstoque.RecNo);
end;
end.
Tremonti
Curtidas 0