对SQL Server2005的EX preSS以多种形式在C#中使用应用程序角色应用程序、多种、角色、形式

2023-09-03 07:21:01 作者:高大上

我有一个C#.net WinForms应用程序有两种形式使用该连接字符串连接到SQL EX preSS。

I have a C# .net winforms application with two forms which connects to sql express using connection string.

Form1中连接到SQL使用受信任的连接。连接字符串是由于在app.config文件。

form1 connects to sql with trusted connection. connection string is given in the app.config file.

现在,在Form1上的一个按钮,点击它改变了连接到应用程序角色凭据。

now, a button on form1 is clicked which changes the connection to application role credentials.

现在,窗口2打开和放大器;它具有点击时需要使用的应用程序角色设置创建数据库的一个按钮。

now, form2 opens & it has a button which when clicked needs to create a database using the application role settings.

让我怎么使用在Form1上创建到窗口2应用程序角色设置。因为执行创建数据库查询窗体2需要一个新的连接对象。

so how do i use the application role settings created in form1 into form2. because to execute the database creation query the Form2 needs a new connection object.

所以,我一定要添加其他app.config文件还是什么别的?

So, do i have to add another app.config file or what else?

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

------------------------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 ------------ ------------------

--------------------------------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 ----------------------------- -----

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

我的app.config文件:

my app.config file :

`XML版本=1.0编码=UTF-8

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

配置

的appSettings     添加键=ApplicationTitle值=设置数据库,表和权限     添加键=ConnectionString的          值=服务器=本地主机; Trusted_Connection =真   的appSettings

appSettings add key="ApplicationTitle" value="Setup Database , Tables and Permissions" add key="ConnectionString" value="Server=localhost; Trusted_Connection=true" appSettings

配置

推荐答案

使用 SqlConnectionStringBuilder 修改连接参数,然后打开到数据库的新连接。

Use a SqlConnectionStringBuilder to modify the connection parameters and then open a new connection to the 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();
            }

        }
    }