Programmer:Create BCE.Data.DBSetting with Database Info: Difference between revisions

From AutoCount Resource Center
Content added Content deleted
No edit summary
mNo edit summary
 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__

==Introduction==
{{Note|This example uses {{DevExpress}} components.}}
{{Note|This example uses {{DevExpress}} components.}}
When create a form Setting for user to maintain which Database to link/integrate,
When create a form Setting for user to maintain which Database to link/integrate,
often we do not know where to obtain the account book setting in AutoCount Accounting.
often we do not know where to obtain the account book setting in AutoCount Accounting.<br />
This example shows how to get '''Database Info''' that is maintained and stored in AutoCount Accounting.
This example shows how to get '''Database Info''' that is maintained and stored in AutoCount Accounting,<br />by using the class of '''BCE.AutoCount.DatabaseManagement'''.
{{SourceDownload|link=https://drive.google.com/open?id=1FB8rtZvF04zd4uUnniOosKqSccuxiOxZ|remark=(Visual Studio 2015)|Download Sample Project|Sample to illustrate how to get Database Info that is stored in Database Management File.}}
[[File:ProgramDBSetDBInfo.png|link=]]<br />
<br /><br />

==Obtain Database Info via DatabaseManager==
===References of AutoCount Accounting 1.8===
{{BaseReferenceAC18}}
<br /><br />


1. Create a class to store Selected Database Info<br />
1. Create a class to store Selected Database Info<br />
Line 16: Line 25:
}
}
</syntaxhighlight>
</syntaxhighlight>
<br /><br />

2. Create a {{DevExpress}} Lookup Edit control in the class
2. Create a {{DevExpress}} Lookup Edit control in the '''form class'''


<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
Line 25: Line 34:
private SelectedDatabaseInfo mydbInfo;
private SelectedDatabaseInfo mydbInfo;
</syntaxhighlight>
</syntaxhighlight>
<br /><br />

3. Call a method to Load '''Database Info''' into Lookup Edit.
3. Add a method that will Load '''Database Info''' into the '''LookupEdit'''.
:You may use other control to get user selection. Below example is using LookupEdit of DevExpress control.
:You may use other control to get user selection. Below example is using LookupEdit of DevExpress control.
:Highlighted lines are the key to obtain '''Database Info''' that has been maintained in AutoCount Accounting 1.8.
:Highlighted lines are the key to obtain '''Database Info''' that has been maintained in AutoCount Accounting 1.8.
<br />
:There are two posibilities:-
:# Get '''Database Info''' after user has login, such as when develop an inter-billing system.
:# Get '''Database Info''' before user login, such as a standalone program that needs to access database info.
===Obtain '''Database Info''' after user login===
This method is only possible after user has login into AutoCount Accounting, else the database info table will be empty.
<syntaxhighlight lang="csharp" highlight="4-7">
<syntaxhighlight lang="csharp" highlight="4-7">
internal void InitiateDatabaseInfoLookup(System.Windows.Forms.Control parent)
internal void InitiateDefaultDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
{
//Get the database info that has been maintained in AutoCount Accounting
//Get the database info that has been attached in AutoCount Accounting
DataTable dtDBInfo =
DataTable dtDBInfo =
BCE.AutoCount.DatabaseManagement.Default.DatabaseManager
BCE.AutoCount.DatabaseManagement.Default.DatabaseManager
Line 38: Line 53:
false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });
false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });


//Set the property of LookupEdit control
lueDatabase.Properties.DataSource = dtDBInfo;
lueDatabase.Properties.DataSource = dtDBInfo;
lueDatabase.Properties.ValueMember = "DatabaseName";
lueDatabase.Properties.ValueMember = "DatabaseName";
lueDatabase.Properties.DisplayMember = "CompanyName";
lueDatabase.Properties.DisplayMember = "CompanyName";
lueDatabase.Properties.NullText = "";
lueDatabase.Properties.NullText = "";
lueDatabase.Properties.PopupWidth = 500;
lueDatabase.Location = new System.Drawing.Point(10, 10);
lueDatabase.Location = new System.Drawing.Point(10, 10);
lueDatabase.Size = new System.Drawing.Size(300, lueDatabase.Size.Height);
lueDatabase.Size = new System.Drawing.Size(200, lueDatabase.Size.Height);


//Add LookupEdit Control to Form or Panel, depends on the parent control
parent.Controls.Add(lueDatabase);
parent.Controls.Add(lueDatabase);
}
}
</syntaxhighlight>
</syntaxhighlight>


===Obtain '''Database Info''' before user login===
To get the database info table before user login, '''LoadDatabaseManagementFile(...)''' method needs to be called.
<syntaxhighlight lang="csharp" highlight="8,11-13">
private void InitiateDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
//Set the path to where the Database Info file is. (dmf: Database Management File)
string appDataPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string defaultDBInfoFile = System.IO.Path.Combine(appDataPath + "\\AutoCount\\Accounting", "A2006.dmf");

//Get the database info that has been maintained in AutoCount Accounting
BCE.AutoCount.DatabaseManagement dbMgmt = new BCE.AutoCount.DatabaseManagement();

//Load Database Info from Database Management File at designated path
dbMgmt.LoadDatabaseManagementFile(defaultDBInfoFile, false);
DataTable dtDBInfo = dbMgmt.DatabaseManager.DatabaseInfoTable.DefaultView.ToTable(
false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });

//Set the property of LookupEdit control
lueDatabase.Properties.DataSource = dtDBInfo;
lueDatabase.Properties.ValueMember = "DatabaseName";
lueDatabase.Properties.DisplayMember = "CompanyName";
lueDatabase.Properties.NullText = "";
lueDatabase.Properties.PopupWidth = 500;
lueDatabase.Location = new System.Drawing.Point(10, 10);
lueDatabase.Size = new System.Drawing.Size(200, lueDatabase.Size.Height);

//Add LookupEdit Control to Form or Panel, depends on the parent control
parent.Controls.Add(lueDatabase);
}
</syntaxhighlight>

<br /><br />
4. On Form closing or when perform saving, store the database info.
4. On Form closing or when perform saving, store the database info.
: Sample code here stores in an object of SelectedDatabaseInfo.
: Sample code here stores in an object of SelectedDatabaseInfo.
<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
private void OnFormCloseOrOnSaveSetting()
private void OnFormClosingOrOnButtonClickSave()
{
{
DataRowView row = lueDatabase.GetSelectedDataRow() as DataRowView;
DataRowView row = lueDatabase.GetSelectedDataRow() as DataRowView;
Line 62: Line 112:
mydbInfo.DBName = row["DatabaseName"].ToString();
mydbInfo.DBName = row["DatabaseName"].ToString();
mydbInfo.SvrName = row["ServerName"].ToString();
mydbInfo.SvrName = row["ServerName"].ToString();

//Refer to number 5
SaveSetting(mydbInfo);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>
<br /><br />

5. Write your algorithm to save the selected Database Info.
5. Write your algorithm to save the selected Database Info.
<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
Line 77: Line 130:
}
}
</syntaxhighlight>
</syntaxhighlight>
<br /><br />

6. Write some code to load the selected Database Info.
6. Write some code to load the selected Database Info.
<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
internal SelectedDatabaseInfo LoadSetting()
internal SelectedDatabaseInfo LoadSetting()
{
{
SelectedDatabaseInfo dbInfo = new SelectedDatabaseInfo();
//Write loading code here

//Write loading code here, and assign value to dbInfo.

return dbInfo;
}
}
</syntaxhighlight>
</syntaxhighlight>


<br /><br />
7. Get BCE.Data.'''DBSetting''' with the selected database info.<br />
: The return value of DBSetting is '''null''', if '''Database Info''' to connect to database server is invalid.


<syntaxhighlight lang="csharp" highlight="9">
7. Get BCE.Data.DBSetting with the selected database info.
The return value of DBSetting may be null, if connection info is wrong.

<syntaxhighlight lang="csharp" highlight="8">
internal BCE.Data.DBSetting GetConnectionToAutoCount(SelectedDatabaseInfo dbInfo)
internal BCE.Data.DBSetting GetConnectionToAutoCount(SelectedDatabaseInfo dbInfo)
{
{
if (dbInfo == null)
if (dbInfo == null)
{
{
//throw error.
//throw error;
//or log error and return null
//or log error and return null
}
}
Line 102: Line 159:
}
}
</syntaxhighlight>
</syntaxhighlight>
<br /><br />

[[Category:Programmer]]
[[Category:Programmer]]
[[Category:API]]
[[Category:API]]

Latest revision as of 01:29, 1 June 2018

Introduction

This example uses DevExpress components.
When create a form Setting for user to maintain which Database to link/integrate,
often we do not know where to obtain the account book setting in AutoCount Accounting.
This example shows how to get Database Info that is maintained and stored in AutoCount Accounting,
by using the class of BCE.AutoCount.DatabaseManagement.
Download Sample Project (Visual Studio 2015)

Sample to illustrate how to get Database Info that is stored in Database Management File.




Obtain Database Info via DatabaseManager

References of AutoCount Accounting 1.8

BCE.AutoCount.dll
BCE.AutoCount.CommonAccounting.dll
BCE.AutoCount.MainEntry.dll
BCE.Utils.dll
BCE.Utils.UI.dll



1. Create a class to store Selected Database Info
[Optional, you may direct save the value to file or database without this class]

internal class SelectedDatabaseInfo
{
    public string CompanyName { get; set; }
    public string Remark { get; set; }
    public string DBName { get; set; }
    public string SvrName { get; set; }
}



2. Create a DevExpress Lookup Edit control in the form class

private DevExpress.XtraEditors.LookUpEdit lueDatabase = new DevExpress.XtraEditors.LookUpEdit();

//Object mydbInfo is optional, if the setting of database info is directly write to file or database
private SelectedDatabaseInfo mydbInfo;



3. Add a method that will Load Database Info into the LookupEdit.

You may use other control to get user selection. Below example is using LookupEdit of DevExpress control.
Highlighted lines are the key to obtain Database Info that has been maintained in AutoCount Accounting 1.8.


There are two posibilities:-
  1. Get Database Info after user has login, such as when develop an inter-billing system.
  2. Get Database Info before user login, such as a standalone program that needs to access database info.

Obtain Database Info after user login

This method is only possible after user has login into AutoCount Accounting, else the database info table will be empty.

internal void InitiateDefaultDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
    //Get the database info that has been attached in AutoCount Accounting
    DataTable dtDBInfo = 
        BCE.AutoCount.DatabaseManagement.Default.DatabaseManager
        .DatabaseInfoTable.DefaultView.ToTable(
            false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });

    //Set the property of LookupEdit control
    lueDatabase.Properties.DataSource = dtDBInfo;
    lueDatabase.Properties.ValueMember = "DatabaseName";
    lueDatabase.Properties.DisplayMember = "CompanyName";
    lueDatabase.Properties.NullText = "";
    lueDatabase.Properties.PopupWidth = 500;
    lueDatabase.Location = new System.Drawing.Point(10, 10);
    lueDatabase.Size = new System.Drawing.Size(200, lueDatabase.Size.Height);

    //Add LookupEdit Control to Form or Panel, depends on the parent control
    parent.Controls.Add(lueDatabase);
}

Obtain Database Info before user login

To get the database info table before user login, LoadDatabaseManagementFile(...) method needs to be called.

private void InitiateDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
    //Set the path to where the Database Info file is. (dmf: Database Management File)
    string appDataPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    string defaultDBInfoFile = System.IO.Path.Combine(appDataPath + "\\AutoCount\\Accounting", "A2006.dmf");

    //Get the database info that has been maintained in AutoCount Accounting
    BCE.AutoCount.DatabaseManagement dbMgmt = new BCE.AutoCount.DatabaseManagement();

    //Load Database Info from Database Management File at designated path
    dbMgmt.LoadDatabaseManagementFile(defaultDBInfoFile, false);
    DataTable dtDBInfo = dbMgmt.DatabaseManager.DatabaseInfoTable.DefaultView.ToTable(
            false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });

    //Set the property of LookupEdit control
    lueDatabase.Properties.DataSource = dtDBInfo;
    lueDatabase.Properties.ValueMember = "DatabaseName";
    lueDatabase.Properties.DisplayMember = "CompanyName";
    lueDatabase.Properties.NullText = "";
    lueDatabase.Properties.PopupWidth = 500;
    lueDatabase.Location = new System.Drawing.Point(10, 10);
    lueDatabase.Size = new System.Drawing.Size(200, lueDatabase.Size.Height);

    //Add LookupEdit Control to Form or Panel, depends on the parent control
    parent.Controls.Add(lueDatabase);
}



4. On Form closing or when perform saving, store the database info.

Sample code here stores in an object of SelectedDatabaseInfo.
private void OnFormClosingOrOnButtonClickSave()
{
    DataRowView row = lueDatabase.GetSelectedDataRow() as DataRowView;
    if (row != null)
    {
        mydbInfo = new SelectedDatabaseInfo();
        mydbInfo.CompanyName = row["CompanyName"].ToString();
        mydbInfo.Remark = row["Remark"].ToString();
        mydbInfo.DBName = row["DatabaseName"].ToString();
        mydbInfo.SvrName = row["ServerName"].ToString();

        //Refer to number 5
        SaveSetting(mydbInfo);
    }
}



5. Write your algorithm to save the selected Database Info.

internal void SaveSetting(SelectedDatabaseInfo dbInfo)
{
    if (dbInfo == null)
    {
        //throw or log error.
    }
    //Write saving code here
}



6. Write some code to load the selected Database Info.

internal SelectedDatabaseInfo LoadSetting()
{
    SelectedDatabaseInfo dbInfo = new SelectedDatabaseInfo();

    //Write loading code here, and assign value to dbInfo.

    return dbInfo;
}



7. Get BCE.Data.DBSetting with the selected database info.

The return value of DBSetting is null, if Database Info to connect to database server is invalid.
internal BCE.Data.DBSetting GetConnectionToAutoCount(SelectedDatabaseInfo dbInfo)
{
    if (dbInfo == null)
    {
        //throw error;
        //or log error and return null
    }

    return new BCE.Data.DBSetting(BCE.Data.DBServerType.SQL2000, dbInfo.SvrName, dbInfo.DBName);
}



Go to menu

Go to top
Resources For AutoCount Software Developers