Saturday, September 24, 2011

How to list all database objects in Sqlite database

Execute the following query and you will see all the table definitions and view definition.

select *
from sqlite_master


Reference:
http://www.sqlite.org/faq.html#q7 

Tuesday, September 13, 2011

Detecting the parent Form from Component class

Component is a class that allows you to build non-visual control. For example, Timer class. By the problem is that it does not have a intuitive way to get the parent control.

Below is the code that will get the parent control reference and save the reference during the design time. Please take note the following code will work when you are dragging a control from the Toolbox onto the Form. For those Component that has already dropped on the Form before you added codes below, it will not work.

private ContainerControl _containerControl = null;
        public ContainerControl ContainerControl
        {
            get { return _containerControl; }
            set { _containerControl = value; }
        }

        public override ISite Site
        {
            get { return base.Site; }
            set
            {
                base.Site = value;
                if (value == null)
                {
                    return;
                }
                
                IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host != null)
                {
                    IComponent componentHost = host.RootComponent;
                    if (componentHost is ContainerControl)
                    {
                        ContainerControl = componentHost as ContainerControl;
                    }
                }
            }
        }

Thursday, September 8, 2011

Getting the Windows system information

In case you want to find out the Windows system information such as printer configuration or network configuration, you may have to learn the WMI (Windows Management Instrumentation).

.Net comes with System.Management.ManagementObjectSearcher class which helps in digging the system information.

To find out more about WMI, visit the following URL:

http://wilsonmar.com/1wmiwbem.htm

To download the tool on generating C# code, visit the following URL:

http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=8572

To see the example on how to use the WMI tool, visit the following URL:

http://www.c-sharpcorner.com/UploadFile/scottlysle/UsingWMI04052007222505PM/UsingWMI.aspx

Registering data provider at runtime

In .Net, you may either register new data provider in the app.config file or register the data provider programmatically.
static bool _has_reg_sqlite_provider = false;

        public static void RegisterSqliteProvider()
        {
            if (_has_reg_sqlite_provider)
            {
                return;
            }

            var dataSet = System.Configuration.ConfigurationManager.GetSection("system.data") as System.Data.DataSet;

            if ((dataSet != null)
                && (dataSet.Tables.Count > 0))
            {
                DataRow[] rows = dataSet.Tables[0].Select("InvariantName='System.Data.SQLite'");

                if ((rows == null)
                    || (rows.Length == 0))
                {
                    dataSet.Tables[0].Rows.Add("SQLite Data Provider",
                        "SQLite Data Provider",
                        "System.Data.SQLite",
                        "System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.74.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
                    );

                    _has_reg_sqlite_provider = true;
                }
            }
        }