【C#】SQL Server數(shù)據(jù)庫表/視圖/存儲過程的所有者并非dbo以及修復賬號孤立問題
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
:SQL Server數(shù)據(jù)庫表/視圖/存儲過程的所有者并非dbo或存在賬號孤立問題時,錯誤表現(xiàn)如下:
//獲取數(shù)據(jù)庫連接字符串
ConnectionString = "data source=(local);initial catalog=clicksun;user id=sa;pwd=clicksun$193$631;Connection Timeout=30";
//創(chuàng)建連接對象
SqlConn = new SqlConnection(ConnectionString);
//打開數(shù)據(jù)庫,修復賬號clicksun孤立問題
try
{
SqlConn.Open();
string ComText = "sp_change_users_login 'Auto_Fix', 'clicksun', NULL, 'clicksun2010'";
SqlCommand Comm = new SqlCommand(ComText, SqlConn);
Comm.ExecuteNonQuery();
SqlConn.Close();
}
catch (Exception) { }
//修復數(shù)據(jù)庫表、視圖、存儲過程所有者不是dbo問題
try
{
SqlConn.Open();
string sqlMessage = "select (user_name(uid)+'.'+name) as uname from sysobjects where status>=0 and xtype in('U','V','P') and user_name(uid)<>'dbo'";
SqlCommand sCmd = new SqlCommand(sqlMessage, SqlConn);
SqlDataReader sdr = sCmd.ExecuteReader();
string updateDBOwner = "";
while (sdr.Read()) { updateDBOwner += "exec sp_changeobjectowner '" + sdr.GetValue(0).ToString() + "','dbo';"; }
sdr.Close();
SqlConn.Close();
if (updateDBOwner != "")
{
SqlConn.Open();
SqlCommand Comm = new SqlCommand(updateDBOwner, SqlConn);
Comm.ExecuteNonQuery();
SqlConn.Close();
}
}
catch (Exception) { }
該文章在 2021/6/5 10:42:10 編輯過 |
關鍵字查詢
相關文章
正在查詢... |