Introduction
Problem definition: when you need to export data from the client application you write a lot of code to do it. More than this when you need to export data from a slower client there may be a problem.
The solution is to create an application server to do the job for the slower clients. But what if your client is strong enough and doesn't need to create the export file on the server? Then you set your client to do the export by himself.
My intention is to solve the old issue of exporting data from the database.
Library explanation
Comutility.dll is used for managing a COM server connection and administrating the catalog.
Database.dll is used for managing the database connection.
DatabaseExportServer.dll is the COM server responsible for getting the data from the database and exporting them in different formats.
DatabaseExport.dll is the components which can be embedded into your application (the interface for the clients).
Installcomserver.exe is a useful application for installing the COM server.
And testDatabaseExport
, testDatabaseExportCCharp
are examples for using the library.
For having a background to understanding this article you need to know: COM, COM+, ADO, ODBC, STL and ATL.
UML class diagram for the export structure.
Using The Code
Using the components from an ATL dialog project you need to include the file ../include/includeExportClient.h into your project.
The namespace ATL_PROJECT_EXPORT
defined into this file contains these methods:
Parameters description: HWND
parent is the handle to the parent which containts the control and unsigned int IDD
is the id of the control.
The solution file for the code is in the databaseExport
directory. For installing the COM server you have a BAT file named installcomserver.bat in the demo project. Also, you have two TXT files: one is com_components.txt (which components belongs to the server) and another Connection.txt which stores information about the connection to the COM and the database servers.
namespace ATL_PROJECT_EXPORT
{
inline void db_address(HWND parent,unsigned int IDD,
const char *val)
inline void db_name(HWND parent,unsignedint IDD,
const char * val)
inline void db_user(HWND parent,unsigned int IDD,
const char * val)
inline void db_password(HWND parent,unsigned int IDD,
const char * val)
inline void ComAddress(HWND parent,unsigned int IDD,
const char * val)
inline void ComDomain(HWND parent,unsigned int IDD,
const char * val)
inline void ComUser(HWND parent,unsigned int IDD,
const char * val)
inline void ComPassword(HWND parent,unsigned int IDD,
const char * val)
inline void SqlStatement(HWND parent,unsigned int IDD,
const char * val)
inline void ComServer(HWND parent,unsigned int IDD,
bool val)
inline void ClientSideExport(HWND parent,unsigned int IDD,
bool val)
inline void ExportType(HWND parent,unsigned int IDD,
ExportType val)
inline void FileName(HWND parent,unsigned int IDD,
const char* val)
inline void ConnectToExportServer(HWND parent,
unsigned int IDD)
inline void Export(HWND parent,unsigned int IDD)
}
In the example testDatabaseExportCCharp
made in C#, I use a database for the test named TestExport. After importing the databaseExport
control into the project, StudioNet generates this wrapper:
namespace databaseExportLib
{
public enum ExportType
{
Excel = 0,
FoxPro = 1,
Csv = 2,
Pdf = 3,
Access = 4,
Html = 5,
Xml = 6,
}
}
namespace databaseExportLib
{
public enum __DbServerType
{
_SqlMicrosoft = 0,
_MySql= 1,
_Oracle = 2,
}
}
namespace AxdatabaseExportLib
{
[DesignTimeVisible(true)]
public class Axdrb_databaseExport : AxHost
{
public Axdrb_databaseExport();
protected override void AttachInterfaces();
protected override void CreateSink();
protected override void DetachSink();
[DesignerSerializationVisibility(0)]
[DispId(11)]
public virtual bool ClientSideExport { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(5)]
public virtual string ComAddress { get; set; }
[DispId(6)]
[DesignerSerializationVisibility(0)]
public virtual string ComDomain { get; set; }
[DispId(8)]
[DesignerSerializationVisibility(0)]
public virtual string ComPassword { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(10)]
public virtual bool ComServer { get; set; }
[DispId(7)]
[DesignerSerializationVisibility(0)]
public virtual string ComUser { get; set; }
[DispId(1)]
[DesignerSerializationVisibility(0)]
public virtual string db_address { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(2)]
public virtual string db_name { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(4)]
public virtual string db_password { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(3)]
public virtual string db_user { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(16)]
public virtual __DbServerType DbServerType { get; set; }
[DispId(14)]
[DesignerSerializationVisibility(0)]
public virtual ExportType ExportType { get;set; }
[DispId(17)]
[DesignerSerializationVisibility(0)]
public virtual string FileName { get; set; }
[DesignerSerializationVisibility(0)]
[DispId(18)]
public virtual bool IfFileExistsRecreate { get; set; }
[DispId(19)]
[DesignerSerializationVisibility(0)]
public virtual bool MessageConfirmation { get; set; }
[DispId(9)]
[DesignerSerializationVisibility(0)]
public virtual string SqlStatement { get; set; }
[DispId(12)]
[DesignerSerializationVisibility(0)]
public virtual bool StartAtOnCreate { get; set; }
public virtual void Export();
}
C# Example code:
public Form1()
{
InitializeComponent();
InitControl();
}
private void button1_Click(object sender, EventArgs e)
{
if(this.comboBox1.Text=="" || this.comboBox2.Text=="" ||
this.comboBox3.Text=="")
{
MessageBox.Show("Error");
return ;
}
if(this.comboBox1.Text!="SqlMicrosoft")
{
MessageBox.Show("Not yet implemented!");
return ;
}
if(this.comboBox3.Text =="Client Side With ComServer")
{
this.axdrb_databaseExport1.ComServer = true;
this.axdrb_databaseExport1.ClientSideExport= true;
}
else
if (this.comboBox3.Text == "Client Side Without ComServer")
{
this.axdrb_databaseExport1.ComServer= false;
this.axdrb_databaseExport1.ClientSideExport = true;
}
else
{
this.axdrb_databaseExport1.ComServer = true;
this.axdrb_databaseExport1.ClientSideExport= false;
}
if(this.comboBox2.Text=="Excel")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Excel;
else
if(this.comboBox2.Text=="FoxPro")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.FoxPro;
else
if(this.comboBox2.Text=="Csv")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Csv;
else
if(this.comboBox2.Text=="Pdf")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Pdf;
else
if(this.comboBox2.Text=="Access")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Access;
else
if(this.comboBox2.Text=="Html")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Html;
else
if(this.comboBox2.Text=="Xml")
this.axdrb_databaseExport1.ExportType= databaseExportLib.ExportType.Xml;
axdrb_databaseExport1.Export();
}
private void InitControl()
{
this.comboBox1.Text="SqlMicrosoft";
this.comboBox2.Text="Html";
this.comboBox3.Text="Client Side With ComServer";
StreamReader readFile=System.IO.File.OpenText("Connection.txt");
int count = 0;
while (!readFile.EndOfStream)
{
string temp=readFile.ReadLine();
int pos=temp.LastIndexOf("=");
switch (count)
{
case 0:
{
this.axdrb_databaseExport1.db_address=temp.Substring(pos + 1);
break;
}
case 1:
{
this.axdrb_databaseExport1.db_name=temp.Substring(pos + 1);
break;
}
case 2:
{
this.axdrb_databaseExport1.db_user=temp.Substring(pos + 1);
break;
}
case 3:
{
this.axdrb_databaseExport1.db_password=temp.Substring(pos + 1);
break;
}
case 4:
{
this.axdrb_databaseExport1.ComAddress=temp.Substring(pos + 1);
break;
}
case 5:
{
this.axdrb_databaseExport1.ComDomain=temp.Substring(pos + 1);
break;
}
case 6:
{
this.axdrb_databaseExport1.ComUser=temp.Substring(pos + 1);
break;
}
case 7:
{
this.axdrb_databaseExport1.ComPassword=temp.Substring(pos + 1);
break;
}
}
count++;
}
readFile.Close();
this.axdrb_databaseExport1.SqlStatement =
"select * from test_table";
this.axdrb_databaseExport1.FileName = "DefaultName";
this.axdrb_databaseExport1.ConnectToExportServer();
}
Points of Interest
I'm interested in your opinion about my code and the utility of it (also if you use this library please post me a message at the forum below).
History
Version 1.0 on 30.07.2007.