问题
I have a C# .net winforms application with two forms which connects to sql express using connection string.
form1 connects to sql with trusted connection. connection string is given in the app.config file.
now, a button on form1 is clicked which changes the connection to application role credentials.
now, form2 opens & it has a button which when clicked needs to create a database using the application role settings.
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.
So, do i have to add another app.config file or what else?
------------------------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----------------------------------
my app.config file :
`xml version="1.0" encoding="utf-8"
configuration
appSettings add key="ApplicationTitle" value="Setup Database , Tables and Permissions" add key="ConnectionString" value="Server=localhost; Trusted_Connection=true" appSettings
configuration
回答1:
Use a SqlConnectionStringBuilder
to modify the connection parameters and then open a new connection to the DB.
EDIT
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();
}
}
}
来源:https://stackoverflow.com/questions/5114558/using-application-role-of-sql-server2005-express-in-multiple-forms-in-c-sharp