public ReturnCode ConnectToDatabase(string PathToDatabase = "")
        {
            if (PathToDatabase == string.Empty)
            {
                PathToDatabase = Path.Combine(Path.Combine(Application.StartupPath, DefaultFolder), DefaultFileName);
            }

            //MessageBox.Show(PathToDatabase);

            SetPathToDatabase(PathToDatabase);
            ReturnCode code;

            try
            {
                code = CheckDatabaseStructure();
            }
            catch
            {
                return(ReturnCodeFactory.Error("The file you specified is not a SQLite database."));
            }

            if (code.Error)
            {
                return(code);
            }

            if (TableTypes != null)
            {
                TableTypes.Clear();
            }
            try
            {
                TableTypes     = GetTable(DbManager.TnTypes);
                RootFolderName = GetRootFolderName();
            }
            catch (Exception ex)
            {
                RootFolderName = string.Empty;
                return(ReturnCodeFactory.Error("Could not read data from the database file.", ex.Message));
            }
            return(ReturnCodeFactory.Success());
        }
 void RTableRegistrationVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
 {
     if (e.PropertyName == "SelectedScheme")
     {
         TableTypes.Clear();
         if (SelectedScheme != null)
         {
             cti.ThreadProgress.ShowWait();
             using (SqlWork sqlWork = new SqlWork())
             {
                 RTableTypeM rTableType = new RTableTypeM()
                 {
                     TypeName = Rekod.Properties.Resources.PgReg_TablesLayersCatalogs
                 };
                 sqlWork.sql = String.Format(
                     @"SELECT pg.schemaname, pg.tablename, 
                                     EXISTS(SELECT 1 FROM sys_scheme.table_info ti WHERE pg.schemaname = ti.scheme_name AND pg.tablename = ti.name_db ) as is_exsist, 
                                     EXISTS(SELECT 1 FROM geometry_columns WHERE f_table_name = pg.tablename) as is_layer
                                 FROM pg_tables pg WHERE pg.schemaname = '{0}' ORDER BY pg.tablename;", SelectedScheme.SchemeName);
                 sqlWork.ExecuteReader();
                 while (sqlWork.CanRead())
                 {
                     RTableM rTable = new RTableM(SelectedScheme)
                     {
                         Name         = sqlWork.GetString("tablename"),
                         IsRegistered = sqlWork.GetBoolean("is_exsist"),
                         TableType    = sqlWork.GetBoolean("is_layer") ? ERTableType.MapLayer : ERTableType.Data,
                         IsMapLayer   = sqlWork.GetBoolean("is_layer")
                     };
                     rTableType.Tables.Add(rTable);
                 }
                 TableTypes.Add(rTableType);
             }
             using (SqlWork sqlWork = new SqlWork())
             {
                 RTableTypeM rViewType = new RTableTypeM()
                 {
                     TypeName = Rekod.Properties.Resources.PgReg_Views
                 };
                 sqlWork.sql = String.Format(
                     @"SELECT pg.schemaname, pg.viewname, 
                                     EXISTS(SELECT 1 FROM sys_scheme.table_info ti WHERE pg.schemaname = ti.scheme_name AND pg.viewname = ti.name_db ) as is_exsist, 
                                     EXISTS(SELECT 1 FROM geometry_columns WHERE f_table_name = pg.viewname) as is_layer 
                                 FROM pg_views pg WHERE pg.schemaname like '{0}' ORDER BY pg.viewname;", SelectedScheme.SchemeName);
                 sqlWork.ExecuteReader();
                 while (sqlWork.CanRead())
                 {
                     RTableM rTable = new RTableM(SelectedScheme)
                     {
                         Name         = sqlWork.GetString("viewname"),
                         IsRegistered = sqlWork.GetBoolean("is_exsist"),
                         TableType    = sqlWork.GetBoolean("is_layer") ? ERTableType.MapLayer : ERTableType.Data,
                         IsMapLayer   = sqlWork.GetBoolean("is_layer")
                     };
                     rViewType.Tables.Add(rTable);
                 }
                 TableTypes.Add(rViewType);
             }
             cti.ThreadProgress.Close();
         }
     }
 }