Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中以多种形式使用SQLServer2005Express的应用程序角色#_C#_.net_Role - Fatal编程技术网

C# 在c中以多种形式使用SQLServer2005Express的应用程序角色#

C# 在c中以多种形式使用SQLServer2005Express的应用程序角色#,c#,.net,role,C#,.net,Role,我有一个C#.net winforms应用程序,它有两个表单,使用连接字符串连接到sql express form1通过可信连接连接到sql。app.config文件中给出了连接字符串 现在,单击form1上的一个按钮,该按钮将更改与应用程序角色凭据的连接 现在,form2打开了&它有一个按钮,单击该按钮时需要使用应用程序角色设置创建数据库 那么,如何将在form1中创建的应用程序角色设置使用到form2中呢。因为要执行数据库创建查询,Form2需要一个新的连接对象 那么,我必须添加另一个app

我有一个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();
    }

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

    }
}
--------------------------------表格2.CS------------------------------

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

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

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

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

    }
}
我的app.config文件:

`xml version=“1.0”encoding=“utf-8”

配置

应用程序设置 add key=“ApplicationTitle”value=“设置数据库、表和权限” add key=“ConnectionString” value=“Server=localhost;Trusted\u Connection=true” 应用程序设置


配置使用
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();
            }

        }
    }

使用
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,不,通常您打开一个DB连接,运行一个查询,然后关闭连接。换句话说,在应用程序运行时不要保持DB连接打开。@tzup,那么我如何为多个表单共享相同的应用程序角色呢。你能帮我弄清楚确切的代码吗?@sqlchild,不要让数据库连接保持打开状态。建议的方法是打开/运行查询/关闭,无论您有多少操作,因此不要在应用程序运行时保持连接打开。关于app.config,我不确定我是否理解。也许另一个问题更适合讨论这个问题。@tzup,先生,连接池应该用于此任务吗?@tzup,c#.net中的每个表单都需要与sql数据库建立新的连接吗?@sqlchild,不,通常您打开一个DB连接,运行一个查询,然后关闭连接。换句话说,在应用程序运行时不要保持DB连接打开。@tzup,那么我如何为多个表单共享相同的应用程序角色呢。你能帮我弄清楚确切的代码吗?@sqlchild,不要让数据库连接保持打开状态。建议的方法是打开/运行查询/关闭,无论您有多少操作,因此不要在应用程序运行时保持连接打开。关于app.config,我不确定我是否理解。也许另一个问题更适合讨论这件事。