I am connecting to the database with the code below
string connString = @"Data source = test2 ; Database=test1data ; User Id=a ; Password=test1234";
using (SqlConnection sqlConn = new SqlConnection(connString))
{
string sqlQuery = @"SELECT * from Zone";
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
dataGridView1.DataSource = new BindingSource(table, null);
}
But I wanted to create a file to connect that could be changed.
For example:
I'm connecting to a database, but changing this file I'll connect to another database
What I have tried:
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
string sqlQuery = @"SELECT * from Zone";
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
dataGridView1.DataSource = new BindingSource(table, null);
}
And this is the code i tryed in app.config
<add name="test" connectionString="Data Source=.;Initial Catalog=test1data; User Id=a ; Password=test123" providerName="System.Data.SqlClient" />