Programmer:Create BCE.Data.DBSetting with Database Info

From AutoCount Resource Center
Revision as of 10:17, 11 January 2018 by DanielY (talk | contribs) (Created page with " {{Note|This example uses {{DevExpress}} components.}} Create a form Setting for user to maintain which Database to link/integrate 1. Create a DevExpress Lookup Edit object i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Tips icons.png This example uses DevExpress components.

Create a form Setting for user to maintain which Database to link/integrate

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 somewhere]

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.

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

IconAC81.png Go to top
ProgrammerGo.jpg Resources For AutoCount Software Developers