【问题标题】:using application role of sql server2005 express in multiple forms in c#在c#中以多种形式使用sql server 2005的应用程序角色表达
【发布时间】:2011-07-04 03:04:14
【问题描述】:

我有一个 C# .net winforms 应用程序,它有两个使用连接字符串连接到 sql express 的表单。

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

    }
}

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

我的 app.config 文件:

`xml 版本="1.0" 编码="utf-8"

配置

应用设置 add key="ApplicationTitle" value="设置数据库、表和权限" 添加键=“连接字符串” value="服务器=本地主机;Trusted_Connection=true" 应用设置

配置

【问题讨论】:

    标签: c# .net role


    【解决方案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();
                }
    
            }
        }
    

    【讨论】:

    • @tzup,先生,这个任务应该使用连接池吗?
    • @tzup,c#.net 中的每个表单都需要与 sql 数据库建立新连接吗?
    • @sqlchild,不,通常您打开数据库连接,运行查询然后关闭连接。换句话说,不要在您的应用程序运行时保持数据库连接打开。
    • @tzup,那么我应该如何为多个表单共享相同的应用程序角色。你能帮我找出确切的代码吗?
    • @sqlchild,不要保持数据库连接打开。推荐的方法是打开/运行查询/关闭,无论您有多少操作,所以只要您的应用程序正在运行,就不要保持连接打开。关于 app.config,我不确定我是否理解。也许另一个问题会是讨论这个问题的更好地方。
    猜你喜欢
    • 2011-05-30
    • 2023-03-06
    • 1970-01-01
    • 2011-02-17
    • 2011-07-29
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多