2015-04-23 33 views
0

我正在一個windows C#2013項目中有一個SQL Server 2012 express數據庫,我創建了3個應用程序角色,我在我的項目中使用了typed dataset,我搜索了一種方法來激活我的項目中的應用程序角色。如何使用C#中的類型化數據集激活應用程序角色?

我發現了一些信息,但沒有完整的答案,我做了一個代碼,但它無法正常工作,因爲該事件Connection_StateChange不火,這裏是一個類的代碼的Class1在我的項目,我把這個代碼:

public string rl, rlPss; 
//rl, rlPss are variables contain role name and password. 

public void SetAppRole(SqlConnection ta) 
{ 
    if (ta.State==ConnectionState.Open) 
    { 
     ta.Close(); 
    } 
    ta.Open(); 

    ta.StateChange += Connection_StateChange; 
} 

void Connection_StateChange(object sender, StateChangeEventArgs e) 
{ 
    if (e.OriginalState==System.Data.ConnectionState.Closed && e.CurrentState==System.Data.ConnectionState.Open) 
    { 
     System.Data.SqlClient.SqlCommand scm = new SqlCommand(); 
     scm.Connection = (SqlConnection)sender; 
     scm.CommandText = "EXEC sp_setAppRole '" + rl + "' , '" + rlPss + "'"; 
     scm.ExecuteNonQuery(); 
    } 
} 

btnlogin下登錄我把這個代碼:

cl.SetAppRole((System.Data.SqlClient.SqlConnection)tableAdapterManager.Connection); 

MDIParent1 md = new MDIParent1(); 
md.Show(); 

我通過角色名和密碼rl,rlPss。

我跟着這個代碼的執行,我發現事件Connection_StateChange不會被解僱。

+0

的代碼格式確實需要提高,老兄之前註冊的事件。 – skyline75489

+1

我建議您閱讀更多關於命名約定 - http://en.wikipedia.org/wiki/Naming_convention_(programming) – gabriel

回答

0

你改變狀態

public void SetAppRole(SqlConnection ta public void SetAppRole(SqlConnection ta) 
 
    { 
 
     ta.StateChange += Connection_StateChange; 
 
     if (ta.State==ConnectionState.Open) 
 
     { 
 
      ta.Close(); 
 
     } 
 
     ta.Open(); 
 

 
    }

相關問題