2010年9月14日 星期二

在程式中變更ConnectionString

通常使用Membership這類物件時,會建立web.config or app.config來設定連線字串及MembershipProvider
以下範例可以動態的在程式碼中變更資料庫連線字串


private void SetProviderConnectionString(string connectionString)
{
// Set private property of Membership, Role and Profile providers.
//Do not try this at home!!
FieldInfo connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);

if (connectionStringField != null)
connectionStringField.SetValue(Membership.Provider, connectionString);

FieldInfo roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);

if (roleField != null)
roleField.SetValue(Roles.Provider, connectionString);

FieldInfo profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);

if (profileField != null)
profileField.SetValue(ProfileManager.Provider, connectionString);
}

2010年9月7日 星期二

解決MsSQL資料庫重新命名失敗問題

使用Microsoft SQL Server Management Studio
右鍵點選要Rename的資料庫...結果出現5030錯誤
這表示資料庫正被使用中(大概XD)

所以,一個比較直覺的想法就是將資料庫停用,強迫所有使用者斷線
然後再重新將資料庫啟用

首先在要Rename的資料庫(右鍵)->工作->卸離,勾選卸除連接,然後按確定
這樣database就會被卸載了。

然後資料庫(右鍵)->附加->選擇剛剛被卸載的資料庫檔案,將資料庫掛回去
這樣database應該就可以rename了。

ps. sp_who這個SQL指令可以查出database正在被那些程序使用