//.................................................................................................
        public void ToSql(string ServerName, string DatabaseName = "ifcSQL_instance", eWriteMode WriteMode = eWriteMode.CreateNewProject, int ProjectId = 0)
        {
            ENTITY.ifcSqlInstance = new ifcSQL._ifcSQL_for_ifcSQL_instance(ServerName: ServerName, DatabaseName: DatabaseName);
            ifcSQL._ifcSQL_for_ifcSQL_instance ifcSQL = ENTITY.ifcSqlInstance;
            long LastGlobalId = 0;

            ifcSQL.conn.Open();
            if (WriteMode == eWriteMode.CreateNewProject)
            {
                ifcSQL.ExecuteNonQuery("app.NewProject '" + Header.name + "'"); ProjectId = 0;
            }
            if (ProjectId == 0)
            {
                ProjectId = ifcSQL.ExecuteIntegerScalar("SELECT cp.ProjectId()");
            }
            else
            {
                ifcSQL.ExecuteNonQuery("app.SelectProject " + ProjectId);
            }
            int EntityCount = ifcSQL.ExecuteIntegerScalar("SELECT count(*) from cp.Entity"); if (WriteMode == eWriteMode.OnlyIfEmpty)

            {
                if (EntityCount > 0)
                {
                    ifcSQL.conn.Close(); throw new NetSystem.Exception("Project with ProjectId=" + ProjectId + " is not empty while using eWriteMode.OnlyIfEmpty");
                }
            }

            if (WriteMode == eWriteMode.DeleteBeforeWrite)
            {
                ifcSQL.ExecuteNonQuery("app.DeleteProjectEntities " + ProjectId);
            }
            ifcSQL.ExecuteNonQuery("ifcProject.NewLastGlobalId " + ProjectId + ", " + ifc.Repository.CurrentModel.EntityList.Count);
            LastGlobalId = ifcSQL.ExecuteLongScalar("SELECT ifcProject.LastGlobalId(" + ProjectId + ")");

            ifcSQL.conn.Close();



            long CurrentGlobalId = LastGlobalId - ifc.Repository.CurrentModel.EntityList.Count;

            FillTables(ProjectId: ProjectId, StartGlobalId: LastGlobalId - ifc.Repository.CurrentModel.EntityList.Count + 1);

            ifcSQL.conn.Open();
            foreach (TableBase tb in OrderedInsertList)
            {
                if (tb.Count > 0)
                {
                    tb.BulkInsert();
                }
            }
            ifcSQL.conn.Close();
        }//................................................................................................