firstly you shuold know about application files.
APP.config files
Configurations files make your life easier.
In an APP.config file, information is stored as Key-Value pairs.
Each application can have a configuration file, which is actually an XML file.
Any text editor can be used to open the configuration file and change values.
The application will load the values from this configuration file.
App.Config files are not even use for connection string, they have several other benifits.
Now do it practically:
server = server name ; uid = user id of your database ; pwd = password of your database
databae = name of your database
Now you can code your Form like :( in C#.net)
Open your form drag a Button on your form and named it btnConnect.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Configuration; //Must add this name space (system.Configuration)
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ConnectionString
{
public partial class Form1 : Form
{
// this line communicate with App.Config File
string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];
// your query
string query = "SELECT * FROM Registration;";
SqlDataAdapter sqlDA;
DataSet dS;
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
//filling dataset
try
{
dS = new DataSet();
SqlConnection sqlCon = new SqlConnection(dbPath);
SqlCommand sqlComm = new SqlCommand(query, sqlCon);
sqlDA = new SqlDataAdapter(sqlComm);
sqlDA.Fill(dS, "table1");
MessageBox.Show("Data has been Filled in Dataset");
}
//If error Occur in filling DataSet then this code will be executed
catch (Exception ex)
{
MessageBox.Show("Error Occured" + ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
APP.config files
Configurations files make your life easier.
In an APP.config file, information is stored as Key-Value pairs.
Each application can have a configuration file, which is actually an XML file.
Any text editor can be used to open the configuration file and change values.
The application will load the values from this configuration file.
App.Config files are not even use for connection string, they have several other benifits.
- If you need to change the server then you will have to change the connection string as well. So,there is no need change your code.
- You just simply change or replace the connection string in (App.Config) Configuration file.Or replace whole Configuration file.
- The configuration file for VB.Net or C#.Net will remain same.
Now do it practically:
- App.config files are not automatically created when you create a Windows application.
- If you need aconfiguration file for your application, open your project in VS.NET, go to the 'Solution Explorer' and right click on the project name. Choose Add -> Add new item from the menu and select 'Application Configuration file' from the list of choices. This will create an app.config file for you in the application root.
- Open app.config file then replace all its code by the following code (donot leave a line or white space).
server = server name ; uid = user id of your database ; pwd = password of your database
databae = name of your database
Now you can code your Form like :( in C#.net)
Open your form drag a Button on your form and named it btnConnect.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Configuration; //Must add this name space (system.Configuration)
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ConnectionString
{
public partial class Form1 : Form
{
// this line communicate with App.Config File
string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];
// your query
string query = "SELECT * FROM Registration;";
SqlDataAdapter sqlDA;
DataSet dS;
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
//filling dataset
try
{
dS = new DataSet();
SqlConnection sqlCon = new SqlConnection(dbPath);
SqlCommand sqlComm = new SqlCommand(query, sqlCon);
sqlDA = new SqlDataAdapter(sqlComm);
sqlDA.Fill(dS, "table1");
MessageBox.Show("Data has been Filled in Dataset");
}
//If error Occur in filling DataSet then this code will be executed
catch (Exception ex)
{
MessageBox.Show("Error Occured" + ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}