1

接続文字列を使用してSQLExpressに接続する2つのフォームを持つC#.netwinformsアプリケーションがあります。

form1は信頼できる接続でSQLに接続します。接続文字列はapp.configファイルで指定されます。

ここで、form1のボタンをクリックして、アプリケーションロールの資格情報への接続を変更します。

これで、form2が開き、クリックするとアプリケーションロール設定を使用してデータベースを作成する必要があるボタンが表示されます。

では、form1で作成されたアプリケーションロール設定をform2に使用するにはどうすればよいですか。データベース作成クエリを実行するには、Form2に新しい接続オブジェクトが必要なためです。

だから、私は別のapp.configファイルを追加する必要がありますか、それとも他に何がありますか?

------------------------編集済み------------------------- ---------------------------

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();

    }
}

---------------編集済み-1-------------------------------- -

私のapp.configファイル:

`xml version =" 1.0 "encoding =" utf-8 "

構成

appSettings add key = "ApplicationTitle" value = "Setup Database、Tables andPermissions" add key = "ConnectionString" value = "Server = localhost; Trusted_Connection = true" appSettings

構成

4

1 に答える 1

1

を使用しSqlConnectionStringBuilderて接続パラメータを変更してから、DBへの新しい接続を開きます。

編集

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();
            }

        }
    }
于 2011-02-25T07:13:36.110 に答える