Globalized connection string meant for that if you are designing a vast application that having so many windows forms and u need to control all the application using an app.config file. So here is just a demo to create a Globalized connection string for distribution of similar connection string throughout your application.
- First you have to attach an assembly named System.configuration for using the facilities of app.config file.
- Go to the solution explorer and add the database from Add new item dialog box that will with the .mdf extension and by default it will be Database1.mdf.
- After adding the database in the solution explorer there will be an app.config file will be added into the solution explorer.
- Now just go to the app.config file and change name value with any name example: [Your name], that name will represent your connection string on the cs form page.
- Now go the view and open the Server Explorer there will be the Database1.mdf just expand it and right click on the tables and select add new table.
- Now create the table columns there and data types and at last just save the table.
- Now expand the table block form the database1.mdf and right click on the created table now click on the show table data and there you can insert the data into the table.
- After inserting the data into the table just go to the cs code page and add a namespace System.Data.SqlClient for using the sqlconnection class and for the another classes.
- Going to the Design form just add the dataGridView control and a button control.
- Double click the button and write that code to enable globalized string into your form.
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
SqlConnection sc = new SqlConnection();
sc.ConnectionString = ConfigurationManager.ConnectionStrings["<your connection string name from app.config file.>"].ConnectionString.ToString();
SqlCommand cmd = new SqlCommand("<your query>", sc);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dataGridView1.DataSource = ds.Tables.DefaultView;
Now just build and debug the program and when you will click on the button and you will see all the data from the specified table or other kind activities.