none
Add all tables to a Scope RRS feed

  • Question

  • Hi, can we add all the tables by use of looping rather than writing these lines repeatedly per table?

     static void Main(string[] args)
            {
                SqlConnection serverConn = new SqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True");
    
                // define a new scope named ProductsScope
                DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("ProductsScope");
    
                // get the description of the Products table from SyncDB dtabase
                DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Products", serverConn);
    
                // add the table description to the sync scope definition
                scopeDesc.Tables.Add(tableDesc);
    
                // create a server scope provisioning object based on the ProductScope
                SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);
    
                // skipping the creation of table since table already exists on server
                serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
    
                // start the provisioning process
                serverProvision.Apply();
            }

    I tried to use loop but I'm getting errors.

    static void Main(string[] args)
            {
                List<string> tables = new List<string>();
                SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=DatabaseName; Integrated Security=True");
            
                    con.Open();
                    DataTable dt = con.GetSchema("Tables");
                    foreach (DataRow row in dt.Rows)
                    {
                        string tablename = (string)row[2];
                        tables.Add(tablename);
                    }
    
    
                foreach (var table in tables)
                  //  foreach (var table in tables.Where(x=>!x.EndsWith("Scope")))
                {
                    DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(table + "Scope");
                    DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(table, con);
                    scopeDesc.Tables.Add(tableDesc);
                    SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(con, scopeDesc);
                    serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
                    
    
                    if (serverProvision.ScopeExists(table + "Scope"))
                    {
                        DataTable tmp = new DataTable();
                        var reader = con.ExecuteReader("select * from scope_info");     //I'm using dapper here
                        tmp.Load(reader);
                        foreach (DataRow row in tmp.Rows)
                        {
                            if ((string)row[2] == table + "Scope")
                            {
                                SqlSyncScopeDeprovisioning deprovisioningvar = new SqlSyncScopeDeprovisioning(con);
                                deprovisioningvar.DeprovisionScope(table + "Scope");
                                
                            }
                        }
                    }
                    else
                    {
                         serverProvision.Apply();
                    }
                       
                }

    Wednesday, August 9, 2017 5:15 PM

All replies

  • what's the error your getting? and why do you have to query the scope_info table? you can simply deprovision the scope if it exists, why the redundant query to the scope_info table?
    Thursday, August 10, 2017 3:16 AM
    Moderator