private bool new_SQL_Parameter(TableDataItem tdi, DataRow dr, DataColumn dcol, ref string sparname, object o, ref SQL_Parameter par)
        {
            long new_ID = -1;
            sparname = "@par_" + dcol.ColumnName;
            //if (tdi.tbl.TableName.Equals("Atom_cAddress_Org"))
            //{
            //    MessageBox.Show("Atom_cAddress_Org");
            //}
            if (IsForeignKey(tdi, dr, dcol, ref new_ID))
            {
                o = new_ID;
            }
            if (o is string)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Nvarchar, false, o);
            }
            else if (o is bool)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Bit, false, o);
            }
            else if (o is short)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Smallint, false, o);

            }
            else if (o is ushort)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Smallint, false, o);
            }
            else if (o is int)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Int, false, o);
            }
            else if (o is uint)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Int, false, o);

            }
            else if (o is long)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Bigint, false, o);

            }
            else if (o is ulong)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Bigint, false, o);
            }
            else if (o is DateTime)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Datetime, false, o);
            }
            else if (o is byte[])
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Varbinary, false, o);
            }
            else if (o is decimal)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Decimal, false, o);
            }
            else if (o is float)
            {
                par = new SQL_Parameter(sparname, SQL_Parameter.eSQL_Parameter.Float, false, o);
            }
            else if (o is System.DBNull)
            {
                sparname = "null";
            }
            else
            {
                LogFile.Error.Show("ERROR:usrc_Upgrade:new_SQL_Parameter: type = " + o.GetType().ToString() + " not implemented !");
                return false;
            }
            return true;
        }
 private bool IsForeignKey(TableDataItem tdi, DataRow dr, DataColumn dcol, ref long new_ID)
 {
     foreach (Column col in tdi.tbl.Column)
     {
         if (col.fKey != null)
         {
             if (col.Name.Equals(dcol.ColumnName))
             {
                 foreach (TableDataItem xtdi in tdi.fkey_TableDataItem_List)
                 {
                     if (col.fKey.fTable.TableName.Equals(xtdi.tbl.TableName))
                     {
                         object o_Old_ID = dr[dcol.ColumnName];
                         if (o_Old_ID is long)
                         {
                             DataRow[] drs = xtdi.dt.Select("old_ID = " + ((long)o_Old_ID).ToString());
                             if (drs.Count() > 0)
                             {
                                 new_ID = (long)drs[0]["ID"];
                                 return true;
                             }
                         }
                    }
                 }
             }
         }
     }
     return false;
 }
 public TableDataItem(SQLTable xtbl, ref List<DataTable> dt_List,TableDataItem xprev, ref string Err)
 {
     // TODO: Complete member initialization
     tbl = xtbl;
     prev = xprev;
     foreach (Column col in xtbl.Column)
     {
         if (col.fKey != null)
         {
             if (!col.fKey.fTable.TableName.Equals("Atom_WorkPeriod"))
             {
                 TableDataItem tdi = new TableDataItem(col.fKey.fTable, ref dt_List, this, ref Err);
                 if (Err != null)
                 {
                     return;
                 }
                 fkey_TableDataItem_List.Add(tdi);
             }
         }
     }
     if (Find_dt_List(ref dt, dt_List, tbl.TableName))
     {
         return;
     }
     else
     {
         string sql = "select * from " + xtbl.TableName;
         dt = new DataTable();
         dt.TableName = tbl.TableName;
         if (DBSync.DBSync.ReadDataTable(ref dt, sql, ref Err))
         {
             dt.Columns.Add("old_ID", typeof(long));
             dt_List.Add(dt);
         }
     }
 }
        private object UpgradeDB_1_04_to_1_05(object obj, ref string Err)
        {
            Check_DB_1_04();
            m_Old_tables_1_04_to_1_05 = new Old_tables_1_04_to_1_05();
            if (m_Old_tables_1_04_to_1_05.Read())
            {
                m_eUpgrade = eUpgrade.from_1_04_to_105;
                wfp_ui_thread = new Database_Upgrade_WindowsForm_Thread();
                wfp_ui_thread.Start();

                List<DataTable> dt_List = new List<DataTable>();
                string Message_Title = " 1.04 -> 1.05";

                SQLTable tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(PersonData));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                SQLTable xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_PersonData = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }

                TableDataItem_List.Add(dt_PersonData);

                Err = null;
                Message_Title = " 1.04 -> 1.05";
                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(myOrganisation));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_myOrganisation = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_myOrganisation);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(Atom_myOrganisation));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_Atom_myOrganisation = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_Atom_myOrganisation);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(Price_Item));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_Price_Item = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_Price_Item);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(Price_SimpleItem));
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_Price_SimpleItem = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {
                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_Price_SimpleItem);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(OrganisationAccount));
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_OrganisationAccount = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {
                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_OrganisationAccount);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(DocInvoice_ShopB_Item));
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_DocInvoice_ShopB_Item = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {
                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_DocInvoice_ShopB_Item);

                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(DocInvoice_ShopC_Item));
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_DocInvoice_ShopC_Item = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {
                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_DocInvoice_ShopC_Item);

                Err = null;
                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(TangentaTableClass.DBSettings));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_DBSettings = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_DBSettings);

                Err = null;
                tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(TangentaTableClass.BaseCurrency));
                wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + Message_Title);
                wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
                xtbl = new SQLTable(tbl);
                xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
                TableDataItem dt_BaseCurrency = new TableDataItem(xtbl, ref dt_List, null, ref Err);
                if (Err != null)
                {

                    wfp_ui_thread.End();
                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                    return false;
                }
                TableDataItem_List.Add(dt_BaseCurrency);

                wfp_ui_thread.Message(lngRPM.s_BackupOfExistingDatabase.s + DBSync.DBSync.DataBase + " -> " + DBSync.DBSync.DataBase_BackupTemp);

                if (DBSync.DBSync.DB_for_Tangenta.DataBase_Make_BackupTemp())
                {
                    if (DBSync.DBSync.DB_for_Tangenta.DataBase_Delete())
                    {
                        if (DBSync.DBSync.DB_for_Tangenta.DataBase_Create())
                        {
                            wfp_ui_thread.Message(lngRPM.s_ImportData.s);
                            if (Write_TableDataItem_List(m_eUpgrade, m_Old_tables_1_04_to_1_05))
                            {
                                // Correct Item's Units
                                Set_DatBase_Version("1.05");
                                string sql = "update item set Unit_ID=1";
                                object ores = null;
                                if (DBSync.DBSync.ExecuteNonQuerySQL(sql, null, ref ores, ref Err))
                                {
                                    wfp_ui_thread.End();
                                    return true;
                                }
                                else
                                {
                                    wfp_ui_thread.End();
                                    LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_04_to_1_05:TableName=" + tbl.TableName + ";Err=" + Err);
                                    return false;
                                }

                            }
                        }
                    }
                }
                wfp_ui_thread.End();
                return true;
            }
            else
            {
                return false;
            }
        }
        private object UpgradeDB_1_01_to_1_02(object obj, ref string Err)
        {
            wfp_ui_thread = new Database_Upgrade_WindowsForm_Thread();
            wfp_ui_thread.Start();

            List<DataTable> dt_List = new List<DataTable>();
            SQLTable tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(Price_Item));
            wfp_ui_thread.Message("$$$" + lngRPM.s_UpgradeDatabase.s + " 1.01 -> 1.02");
            wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
            SQLTable xtbl = new SQLTable(tbl);
            xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
            TableDataItem dt_Price_Item = new TableDataItem(xtbl, ref dt_List, null, ref Err);
            if (Err != null)
            {

                wfp_ui_thread.End();
                LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_01_to_1_02:TableName=" + tbl.TableName + ";Err=" + Err);
                return false;
            }
            TableDataItem_List.Add(dt_Price_Item);

            tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(Price_SimpleItem));
            wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
            xtbl = new SQLTable(tbl);
            xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
            TableDataItem dt_Price_SimpleItem = new TableDataItem(xtbl, ref dt_List, null, ref Err);
            if (Err != null)
            {
                wfp_ui_thread.End();
                LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_01_to_1_02:TableName=" + tbl.TableName + ";Err=" + Err);
                return false;
            }
            TableDataItem_List.Add(dt_Price_SimpleItem);

            tbl = DBSync.DBSync.DB_for_Tangenta.m_DBTables.GetTable(typeof(OrganisationAccount));
            wfp_ui_thread.Message(lngRPM.s_ReadTable.s + tbl.TableName);
            xtbl = new SQLTable(tbl);
            xtbl.CreateTableTree(DBSync.DBSync.DB_for_Tangenta.m_DBTables.items);
            TableDataItem dt_OrganisationAccount = new TableDataItem(xtbl, ref dt_List, null, ref Err);
            if (Err != null)
            {
                wfp_ui_thread.End();
                LogFile.Error.Show("ERROR:usrc_Upgrade:UpgradeDB_1_01_to_1_02:TableName=" + tbl.TableName + ";Err=" + Err);
                return false;
            }
            TableDataItem_List.Add(dt_OrganisationAccount);

            wfp_ui_thread.Message(lngRPM.s_BackupOfExistingDatabase.s + DBSync.DBSync.DataBase + " -> " + DBSync.DBSync.DataBase_BackupTemp);

            if (DBSync.DBSync.DB_for_Tangenta.DataBase_Make_BackupTemp())
            {
                if (DBSync.DBSync.DB_for_Tangenta.DataBase_Delete())
                {
                    if (DBSync.DBSync.DB_for_Tangenta.DataBase_Create())
                    {
                        wfp_ui_thread.Message(lngRPM.s_ImportData.s);
                        if (Write_TableDataItem_List(m_eUpgrade, m_Old_tables_1_04_to_1_05))
                        {
                            Set_DatBase_Version("1.02");
                        }
                    }
                }
            }
            wfp_ui_thread.End();
            return true;
        }