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

From AutoCount Resource Center
Content added Content deleted
No edit summary
No edit summary
Line 95: Line 95:
if (dbInfo == null)
if (dbInfo == null)
{
{
//throw or log error.
//throw error.
//or log error and return null
}
}



Revision as of 16:07, 11 January 2018

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.

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 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. Call a method to Load Database Info into Lookup Edit.

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.
internal void InitiateDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
    //Get the database info that has been maintained in AutoCount Accounting
    DataTable dtDBInfo = 
        BCE.AutoCount.DatabaseManagement.Default.DatabaseManager
        .DatabaseInfoTable.DefaultView.ToTable(
            false, new string[] { "CompanyName", "Remark", "DatabaseName", "ServerName" });

    lueDatabase.Properties.DataSource = dtDBInfo;
    lueDatabase.Properties.ValueMember = "DatabaseName";
    lueDatabase.Properties.DisplayMember = "CompanyName";
    lueDatabase.Properties.NullText = "";
    lueDatabase.Location = new System.Drawing.Point(10, 10);
    lueDatabase.Size = new System.Drawing.Size(300, lueDatabase.Size.Height);

    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 OnFormCloseOrOnSaveSetting()
{
    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();
    }
}

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()
{
    //Write loading code here
}


7. Get BCE.Data.DBSetting with the selected database info. The return value of DBSetting may be null, if connection info is wrong.

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