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

From AutoCount Resource Center
Content added Content deleted
mNo edit summary
No edit summary
Line 64: Line 64:
</syntaxhighlight>
</syntaxhighlight>


5. Write your algorithm to save the selected Database Info.
5. Write your algorithm to save the selected Database Info for linking/integration.


6. Create BCE.Data.DBSetting object, with the selected database info.
6. Create BCE.Data.DBSetting object, with the selected database info.

Revision as of 10:24, 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 DevExpress Lookup Edit object in the class as private

private DevExpress.XtraEditors.LookUpEdit lueDatabase = new DevExpress.XtraEditors.LookUpEdit();
//this is optional, if the setting of database info is directly write to file or database
private SelectedDatabaseInfo mydbInfo;

2. Call a method to Load the Lookup Edit with data of Database Info.

You may use other control to get user selection. Below example is using LookupEdit of DevExpress control.
internal void InitiateDatabaseInfoLookup(System.Windows.Forms.Control parent)
{
    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);
}

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

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


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

Sample code here will store 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 for linking/integration.

6. Create BCE.Data.DBSetting object, 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 or log error.
    }

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

Go to menu

Go to top
Resources For AutoCount Software Developers