2011-02-25 19 views
1

我有一個C#.net winforms應用程序與兩個連接到sql express使用連接字符串的形式。使用sql server2005 express在c#中的多種形式的應用程序角色

form1通過可信連接連接到sql。連接字符串在app.config文件中給出。

現在,單擊form1上的一個按鈕,它將連接更改爲應用程序角色憑證。

now,form2打開&它有一個按鈕,點擊時需要使用應用程序角色設置創建數據庫。

那麼我如何使用在form1中創建的應用程序角色設置到form2中。因爲要執行數據庫創建查詢,Form2需要一個新的連接對象。

所以,我必須添加另一個app.config文件或其他什麼?

------------------------ EDITED --------------------- -------------------------------

public partial class Form1 : Form 
{ 
    SqlConnection conn = new SqlConnection(); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
      //new code for using conenction string from app.config file added 
       // Read appSettings 
        string title = ConfigurationSettings.AppSettings["ApplicationTitle"]; 
        string connectString = 
        ConfigurationSettings.AppSettings["ConnectionString"]; 
        conn.ConnectionString = connectString; 
      //new code ends 

     conn.Open(); 
     string setapprole = "sp_setapprole 'my_app' , 'app_pass' "; 
     SqlCommand cmd_app = new SqlCommand(setapprole, conn); 
     SqlDataReader approle_reader = cmd_app.ExecuteReader(); 
     approle_reader.Close(); 

     Form2 f2 = new Form2(); 
     f2.Show(); 
    } 

} 

------------ -------------------- FORM2.CS --------------------------- ---

public partial class Form2 : Form 
{ 
    //how to connect to the database, 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string query = "create database new_db"; 
     //I WANT TO USE the conn object here, 
      //& want the connection to use the application role 
     // which was set in Form1.cs 
     SqlCommand cmd = new SqlCommand(query, conn); 
     SqlDataReader createdb = cmd.ExecuteReader(); 

    } 
} 

--------------- EDITED-1 ---------------------- ------------

我的app.config文件:

`XML版本= 「1.0」 編碼= 「UTF-8」

配置

的appSettings 添加關鍵= 「ApplicationTitle」 值= 「安裝程序數據庫,表和權限」 添加鍵= 「ConnectionString的」 value =「Server = localhost; Trusted_Connection =真正的」 的appSettings

配置

回答

1

使用SqlConnectionStringBuilder修改連接參數,然後打開到數據庫的新連接。

編輯

public partial class Form1 : Form 
    { 
     SqlConnection conn = new SqlConnection("Data Source=TODO;Initial Catalog=TODO;Integrated Security=True"); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      using (SqlCommand command = new SqlCommand("sp_setapprole 'my_app' , 'app_pass' ", conn)) 
      { 
       command.CommandType = CommandType.Text; 
       conn.Open(); 
       command.ExecuteNonQuery(); 
      } 
      // The application role is set and remains active until the user disconnects 

      Form2 f2 = new Form2(conn); 
      f2.Show(); 
     } 
    } 

    public partial class Form2 : Form 
    { 
     SqlConnection conn = null; 
     //how to connect to the database, 
     public Form2(SqlConnection conn) 
     { 
      InitializeComponent(); 
      this.conn = conn; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       using (SqlCommand command = new SqlCommand("create database new_db", conn)) 
       { 
        command.CommandType = CommandType.Text; 
        command.ExecuteNonQuery(); 
       } 
      } 
      finally 
      { 
       // Important to close the DB connection (at which point the approle becomes inactive) 
       conn.Close(); 
      } 

     } 
    } 
+0

@tzup,先生,連接池應該用於這項任務嗎? – sqlchild 2011-02-25 07:22:09

+0

@tzup,c#.net中的每個表單都需要一個與sql數據庫建立的新連接嗎? – sqlchild 2011-02-25 07:25:11

+0

@sqlchild,不,通常你打開一個數據庫連接,運行一個查詢,然後關閉連接。換句話說,不要在應用程序運行時保持數據庫連接的打開狀態。 – tzup 2011-02-25 07:33:38

相關問題