PDA

Ver la Versión Completa : Acceder a un DataSet (.NET) desde otra Unidad


SMTZ
15-10-2005, 11:42:28
Buenas, ¿cómo puedo acceder a un componente DataSet (.NET) desde otra unidad? Adjunto un pequeño ejemplo con el error que me da señalado en negrita.

En .NET ¿existen algún componente similar al DataModule o al RemoteDataModule de Delphi Win32?

Gracias.

MiUnidad1
========

Type

MiForm = class(System.Windows.Forms.Form)

//strict private
Public

Componente1: System.Windows.Forms.Label;
Componente2: System.Windows.Forms.Label;
...

MiDataSet: System.Data.DataSet;
MiBdpDataAdapter: Borland.Data.Provider.BdpDataAdapter;
MiBdpConnection: Borland.Data.Provider.BdpConnection;
MiBdpCommand: Borland.Data.Provider.BdpCommand;
DataTable1: System.Data.DataTable;
DataColumn1: System.Data.DataColumn;
DataColumn2: System.Data.DataColumn;
DataColumn3: System.Data.DataColumn;
DataColumn4: System.Data.DataColumn;
DataColumn5: System.Data.DataColumn;
DataColumn6: System.Data.DataColumn;
DataColumn7: System.Data.DataColumn;
DataColumn8: System.Data.DataColumn;
...
End;


MiUnidad2
========

Uses MiUnidad1;

...

Procedure prueba;
Begin

Componente1.Text := 'prueba';
MiDataSet.Tables.... {<-- DESDE la Unidad2 no me reconoce como definido el componente MiDataSet pero sí el Componente1}

End;

__hector
16-10-2005, 05:28:10
Mira a ver si la conversacion sostenida en este hilo (http://www.clubdelphi.com/foros/showthread.php?t=24610) te puede ser de ayuda.

SMTZ
16-10-2005, 11:01:45
Muchas gracias por la información.

SMTZ
16-10-2005, 18:24:45
Me parece que la explicación de ese hilo es para usuarios de Visual Basic, la cual cosa no coincide plenamente con la sintaxis de Delphi. Yo ya tengo creado el dataset como público, que es una de las cosas que ahí se indica, pero nada, sigo sin poder compartir el dataset con otra unidad. Ya me estoy volviendo loco.

__hector
17-10-2005, 15:35:07
mmm, el lenguaje en que esta escrito es en realidad C# y, no debe haber mucha diferencia en el funcionamiento frente a lo que quieres hacer en delphi. Solo tienes que definir el objeto como estatico (static en c#, shared en VB, no se que palabra clave usa delphi) y utilizarlo desde cualquier otro formulario de la forma NombreClase.NombreDataSet.

SMTZ
18-10-2005, 00:25:43
Buenas, al final he optado por crear el dataset a mano. No es lo que me hubiera gustado, que para eso existen los componentes graficos, pero funciona:

=======================

unit WinFormU;

interface

uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data, System.Globalization,
Borland.Data.Provider, DataSetU;

type
TWinForm = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
DataGrid1: System.Windows.Forms.DataGrid;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure InitializeComponent;
{$ENDREGION}
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean); override;
private
{ Private Declarations }
public
constructor Create;
end;

[assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

implementation

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
Self.DataGrid1 := System.Windows.Forms.DataGrid.Create;
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).BeginInit;
Self.SuspendLayout;
//
// DataGrid1
//
Self.DataGrid1.DataMember := '';
Self.DataGrid1.HeaderForeColor := System.Drawing.SystemColors.ControlText;
Self.DataGrid1.Location := System.Drawing.Point.Create(8, 16);
Self.DataGrid1.Name := 'DataGrid1';
Self.DataGrid1.Size := System.Drawing.Size.Create(280, 240);
Self.DataGrid1.TabIndex := 0;
//
// TWinForm
//
Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
Self.ClientSize := System.Drawing.Size.Create(292, 266);
Self.Controls.Add(Self.DataGrid1);
Self.Name := 'TWinForm';
Self.Text := 'WinForm';
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).EndInit;
Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after InitializeComponent call
//
end;

end.

=======================

unit DataSetU;

interface

Uses
System.Data, Borland.Data.Provider;

Type
TMiBD = Record
MiDataSet : DataSet;
MiAdaptador : BdpDataAdapter;
MiConexion : BdpConnection;
MiComando : BdpCommand;

End;

Var
MiBD : TMiBD;

implementation

Begin

With MiBD Do
Begin

MiDataSet := DataSet.Create ( 'MiBD');

MiConexion := BdpConnection.Create;
MiConexion.ConnectionOptions := 'transaction isolation' +
'=ReadCommitted';
MiConexion.ConnectionString := 'database=Ecodomes;asse' +
'mbly=Borland.Data.Oracle, Version=2.0.0.0, Culture=neutral, PublicKeyToke' +
'n=91d62ebb5b0d1b1b;vendorclient=oci.dll;provider=Oracle;username=system;p' +
'assword=cz4lg3';

MiComando := Borland.Data.Provider.BdpCommand.Create;
MiComando.CommandOptions := nil;
MiComando.CommandText := 'select * from usuarios';
MiComando.CommandType := System.Data.CommandType.Text;
MiComando.Connection := MiConexion;
MiComando.ParameterCount := (SmallInt(0));
MiComando.SchemaName := nil;
MiComando.Transaction := nil;
MiCOmando.UpdatedRowSource := System.Data.UpdateRowSource.None;

MiAdaptador := BDPDataAdapter.Create;
MiAdaptador.DataSet := MiDataSet;
MiAdaptador.SelectCommand := MiComando;
MiAdaptador.SelectCommand.CommandText := 'select * from usuarios';
MiAdaptador.Fill(MiDataSet);
MiAdaptador.Active := True;

End;

end.

SMTZ
22-10-2005, 09:18:40
Buenas, al fin he conseguido realizar lo que quería, que es utilizar el componente dataset de la barra de componentes de delphi pero haciendo que este sea público, es decir, que pueda acceder desde otra unidad. La solución ha sido bastante sencilla.

unit UNIDAD1;
===========

Type
MyDS = Class(DataSet)
Public

Constructor Create; Overload;

End;

Constructor MyDS.Create;
Begin

Self.Create('MyDs');

End;


unit WinFormU;
===========

type
TWinForm = class(System.Windows.Forms.Form)
strict private

DataSet1: DataSetU.MyDS; //DataSet1 es el componente utilizado desde la barra de componentes de delphi.

End;

procedure TWinForm.InitializeComponent;
begin

Self.DataSet1 := Unidad1.MyDS.Create;

End;

SMTZ
22-10-2005, 11:22:52
unit WinForm;

{En esta unidad insertamos los componentes BdpConnection1 y BdpAdapter1 de la plaeta de componentes y el DataSet lo creamos manualmente a través de una variable.}

interface

uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data, System.Data.Common, Borland.Data.Provider,
System.Globalization;

type
TWinForm = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
BdpConnection1: Borland.Data.Provider.BdpConnection;
bdpSelectCommand1: Borland.Data.Provider.BdpCommand;
bdpInsertCommand1: Borland.Data.Provider.BdpCommand;
bdpUpdateCommand1: Borland.Data.Provider.BdpCommand;
bdpDeleteCommand1: Borland.Data.Provider.BdpCommand;
BdpDataAdapter1: Borland.Data.Provider.BdpDataAdapter;
Button1: System.Windows.Forms.Button;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure InitializeComponent;
procedure Button1_Click(sender: System.Object; e: System.EventArgs);
{$ENDREGION}
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean); override;
private
{ Private Declarations }
public
constructor Create;
end;

[assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

Var

DatasetPublico : System.Data.DataSet;

implementation

uses WinForm1;

{El datasource del datagrid1 = al dataset creado en la unidad anterior. Este formulario saldrá al presionar al boton1 creado en el formulario anterior.}

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
DataSetPublico := System.Data.DataSet.Create;
Self.BdpConnection1 := Borland.Data.Provider.BdpConnection.Create;
Self.bdpSelectCommand1 := Borland.Data.Provider.BdpCommand.Create;
Self.bdpInsertCommand1 := Borland.Data.Provider.BdpCommand.Create;
Self.bdpUpdateCommand1 := Borland.Data.Provider.BdpCommand.Create;
Self.bdpDeleteCommand1 := Borland.Data.Provider.BdpCommand.Create;
Self.BdpDataAdapter1 := Borland.Data.Provider.BdpDataAdapter.Create;
Self.Button1 := System.Windows.Forms.Button.Create;
(System.ComponentModel.ISupportInitialize(Self.BdpDataAdapter1)).BeginInit;
Self.SuspendLayout;
//
// BdpConnection1
//
Self.BdpConnection1.ConnectionOptions := 'transaction isolation=ReadCommit' +
'ted';
Self.BdpConnection1.ConnectionString := 'assembly=Borland.Data.Oracle, Ver' +
'sion=2.0.0.0, Culture=neutral, PublicKeyToken=91d62ebb5b0d1b1b;vendorclie' +
'nt=oci.dll;database=Ecodomes;provider=Oracle;username=system;password=cz4' +
'lg3';
//
// bdpSelectCommand1
//
Self.bdpSelectCommand1.CommandOptions := nil;
Self.bdpSelectCommand1.CommandText := 'select * from usuarios';
Self.bdpSelectCommand1.CommandType := System.Data.CommandType.Text;
Self.bdpSelectCommand1.Connection := Self.BdpConnection1;
Self.bdpSelectCommand1.ParameterCount := (SmallInt(0));
Self.bdpSelectCommand1.SchemaName := nil;
Self.bdpSelectCommand1.Transaction := nil;
Self.bdpSelectCommand1.UpdatedRowSource := System.Data.UpdateRowSource.None;
//
// bdpInsertCommand1
//
Self.bdpInsertCommand1.CommandOptions := nil;
Self.bdpInsertCommand1.CommandText := nil;
Self.bdpInsertCommand1.CommandType := System.Data.CommandType.Text;
Self.bdpInsertCommand1.Connection := nil;
Self.bdpInsertCommand1.ParameterCount := (SmallInt(0));
Self.bdpInsertCommand1.SchemaName := nil;
Self.bdpInsertCommand1.Transaction := nil;
Self.bdpInsertCommand1.UpdatedRowSource := System.Data.UpdateRowSource.None;
//
// bdpUpdateCommand1
//
Self.bdpUpdateCommand1.CommandOptions := nil;
Self.bdpUpdateCommand1.CommandText := nil;
Self.bdpUpdateCommand1.CommandType := System.Data.CommandType.Text;
Self.bdpUpdateCommand1.Connection := nil;
Self.bdpUpdateCommand1.ParameterCount := (SmallInt(0));
Self.bdpUpdateCommand1.SchemaName := nil;
Self.bdpUpdateCommand1.Transaction := nil;
Self.bdpUpdateCommand1.UpdatedRowSource := System.Data.UpdateRowSource.None;
//
// bdpDeleteCommand1
//
Self.bdpDeleteCommand1.CommandOptions := nil;
Self.bdpDeleteCommand1.CommandText := nil;
Self.bdpDeleteCommand1.CommandType := System.Data.CommandType.Text;
Self.bdpDeleteCommand1.Connection := nil;
Self.bdpDeleteCommand1.ParameterCount := (SmallInt(0));
Self.bdpDeleteCommand1.SchemaName := nil;
Self.bdpDeleteCommand1.Transaction := nil;
Self.bdpDeleteCommand1.UpdatedRowSource := System.Data.UpdateRowSource.None;
//
// BdpDataAdapter1
//
Self.BdpDataAdapter1.DataSet := DataSetPublico;
Self.BdpDataAdapter1.Active := True;
Self.BdpDataAdapter1.DataTable := nil;
Self.BdpDataAdapter1.DeleteCommand := Self.bdpDeleteCommand1;
Self.BdpDataAdapter1.InsertCommand := Self.bdpInsertCommand1;
Self.BdpDataAdapter1.SelectCommand := Self.bdpSelectCommand1;
Self.BdpDataAdapter1.StartRecord := 0;
Self.BdpDataAdapter1.UpdateCommand := Self.bdpUpdateCommand1;
//
// Button1
//
Self.Button1.Location := System.Drawing.Point.Create(80, 104);
Self.Button1.Name := 'Button1';
Self.Button1.TabIndex := 0;
Self.Button1.Text := 'Button1';
Include(Self.Button1.Click, Self.Button1_Click);
//
// TWinForm
//
Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
Self.ClientSize := System.Drawing.Size.Create(292, 266);
Self.Controls.Add(Self.Button1);
Self.Name := 'TWinForm';
Self.Text := 'WinForm';
(System.ComponentModel.ISupportInitialize(Self.BdpDataAdapter1)).EndInit;
Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after InitializeComponent call
//
end;

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin

TWinform1.Create.Show;

end;

end.


unit WinForm1;

interface

uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;

type
TWinForm1 = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
DataGrid1: System.Windows.Forms.DataGrid;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure InitializeComponent;
procedure TWinForm1_Load(sender: System.Object; e: System.EventArgs);
{$ENDREGION}
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean); override;
private
{ Private Declarations }
public
constructor Create;
end;

[assembly: RuntimeRequiredAttribute(TypeOf(TWinForm1))]

implementation

uses WinForm;

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm1.InitializeComponent;
begin
Self.DataGrid1 := System.Windows.Forms.DataGrid.Create;
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).BeginInit;
Self.SuspendLayout;
//
// DataGrid1
//
Self.DataGrid1.DataMember := '';
Self.DataGrid1.HeaderForeColor := System.Drawing.SystemColors.ControlText;
Self.DataGrid1.Location := System.Drawing.Point.Create(16, 16);
Self.DataGrid1.Name := 'DataGrid1';
Self.DataGrid1.Size := System.Drawing.Size.Create(256, 232);
Self.DataGrid1.TabIndex := 0;
//
// TWinForm1
//
Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
Self.ClientSize := System.Drawing.Size.Create(292, 266);
Self.Controls.Add(Self.DataGrid1);
Self.Name := 'TWinForm1';
Self.Text := 'WinForm1';
Include(Self.Load, Self.TWinForm1_Load);
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).EndInit;
Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm1.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;

constructor TWinForm1.Create;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after InitializeComponent call
//
end;

procedure TWinForm1.TWinForm1_Load(sender: System.Object; e: System.EventArgs);
begin

DataGrid1.DataSource := DataSetPublico;

end;

end.

__hector
22-10-2005, 17:07:17
Por favor, en lo adelante, intenta de copiar el codigo delphi dentro de etiquetas de formateo de codigo, tales como [ CODE ] [ / CODE ] y/o [ delphi ] [ /delphi ] (omitiendo los espacios entre los corchetes y la palabra)

Eso hara mas legible el codigo que muestras.

SMTZ
22-10-2005, 17:10:40
Ok, así lo haré. Desconocía esa utilidad.

Saludos.