Home > DeveloperSection > Articles > Creating Globalized Connection String in .Net Framework using Attached Database

Creating Globalized Connection String in .Net Framework using Attached Database


C# C# 
Ratings:
1 Comment(s)
 6162  View(s)
Rate this:

Creating Globalized Connection String in .Net Framework using Attached Database

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.

Creating Globalized Connection String in .Net Framework using Attached Database

·         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.

Code

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using System.Data.SqlClient;

 

namespace AttachedDatabase

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

 

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

sc.Open();

SqlCommand cmd = new SqlCommand("<your query>", sc);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

sda.Fill(ds);

dataGridView1.DataSource = ds.Tables[0].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.

Creating Globalized Connection String in .Net Framework using Attached Database


Creating Globalized Connection String in .Net Framework using Attached Database

By Arun Singh on   5 years ago
Thanks for sharing with us.

Don't want to miss updates? Please click the below button!

Follow MindStick