Ver Mensaje Individual
  #1  
Antiguo 11-09-2008
LoganFenix LoganFenix is offline
Registrado
 
Registrado: sep 2008
Posts: 3
Reputación: 0
LoganFenix Va por buen camino
Guardar datos de ADOquery

Hola soy nuevo en Delphi y estoy tratando de hacer un sistema de consultas jalando una base datos Excel, con este codigo logre que la base de datos se cargue en el Delphi ahora lo que necesito es que se se guarden los valores de cada campo en variables individuales. Desconozco la sentencia que se usa en Delphi para esto. ¿me podria ayudar?

Código Delphi [-]
unit Convertir;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ExtCtrls, DBCtrls, Grids, DBGrids, ComCtrls, StdCtrls, typinfo;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    ListBox1: TListBox;
    StatusBar1: TStatusBar;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    ADOConnection1: TADOConnection;
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;

    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DisplayException(Sender: TObject; E: Exception);
    procedure ConnectToExcel;
    procedure FetchData;
    procedure GetFieldInfo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//uses typinfo; //don't forget

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdoConnection1.LoginPrompt := False;
  AdoQuery1.Connection       := AdoConnection1;
  DataSource1.DataSet        := AdoQuery1;
  DBGrid1.DataSource         := DataSource1;
  DBNavigator1.DataSource    := DataSource1;
  Application.OnException    := DisplayException;
end;

procedure TForm1.DisplayException(Sender: TObject; E: Exception);
begin
  StatusBar1.SimpleText := E.Message;
end;

procedure TForm1.ConnectToExcel;
var strConn :  widestring;
vCell:Variant;
begin
  strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
           'Data Source=' + Edit1.Text + ';' +
           'Extended Properties=Excel 8.0;';

  AdoConnection1.Connected:=False;
  AdoConnection1.ConnectionString:=strConn;
  try
    AdoConnection1.Open;
    AdoConnection1.GetTableNames(ComboBox1.Items,True);
  except
    ShowMessage('Unable to connect to Excel, make sure '+
                'the workbook ' + Edit1.Text + ' exist!');
    raise;
  end;
 // vCell.WrapText := True;
end;(*ConnectToExcel*)


procedure TForm1.FetchData;
begin
  StatusBar1.SimpleText:='';

  ConnectToExcel;

  AdoQuery1.Close;
  AdoQuery1.SQL.Text:=Edit2.Text;
  try
    AdoQuery1.Open;
  except
    ShowMessage('Unable to read data from Excel, '+
                'make sure the query ' + Edit1.Text +
                 ' is meaningful!');
    raise;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  FetchData;
  
  //explained later
  GetFieldInfo;

end;

procedure TForm1.GetFieldInfo;
var
  i      : integer;
  ft     : TFieldType;
  sft    : string;
  fname  : string;
begin
  ListBox1.Clear;
  for i := 0 to AdoQuery1.Fields.Count - 1 do
  begin
    ft := AdoQuery1.Fields[i].DataType;
    sft := GetEnumName(TypeInfo(TFieldType), Integer(ft));
    fname:= AdoQuery1.Fields[i].FieldName;

    ListBox1.Items.Add(Format('%d) NAME: %s TYPE: %s',[1+i, fname, sft]));
  end;
end;


end.

Última edición por LoganFenix fecha: 11-09-2008 a las 16:29:51.
Responder Con Cita