Subsonic: dynamic connections

后端 未结 5 1991
执笔经年
执笔经年 2020-12-17 06:53

I have an ancient mess I\'m trying to shovel into tiers using subsonic. Trouble is, I have this scenario:

When Hal logs in, his login uses database X for lookup data

5条回答
  •  温柔的废话
    2020-12-17 06:59

    Another example of changing providers on the fly (at runtime). This one reads items from one database and saves them to another.

    Use:

    CopyToAnotherDB(Dock.Columns.Id, Dock.BarcodeStringColumn);
    

    Method:

        public static void CopyToAnotherDB(string identifierColumnName, TableSchema.TableColumn fakeDirtyColHack)
            where E : ActiveRecord, new()
        {
            SqlQuery q = new Select().From();
            q.ProviderName = SERVU_PROVIDER;
            IList list = q.ExecuteTypedList();
    
            string connStr = ConfigurationManager.ConnectionStrings[ARCHIVE_PROVIDER].ConnectionString;
            using (SharedDbConnectionScope scope = new SharedDbConnectionScope(connStr))
            {
                foreach (E item in list)
                {
                    int itemID = (int)item.GetColumnValue(identifierColumnName);
                    SqlQuery qry = new Select(identifierColumnName).From().Where(identifierColumnName).IsEqualTo(itemID);
                    int recCount = qry.GetRecordCount();
                    E obj = item.Clone();
    
                    obj.IsNew = recCount == 0; //determines if adding or updating
                    obj.IsLoaded = !obj.IsNew; //determines if adding or updating
                    obj.DirtyColumns.Add(fakeDirtyColHack);
                    obj.Save();
                }
            }
        }
    

提交回复
热议问题