0
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
class CommandExampleSql
{
static void Main()
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
SqlConnection thisConnection = new SqlConnection(
connStr);
SqlCommand thisCommand = new SqlCommand();
try {
thisConnection.Open();
thisCommand.Connection = thisConnection;
thisCommand.CommandText = "...........";// put your query instead of ...
Console.WriteLine("Ready to execute SQL command: {0}", thisCommand.CommandText);
}
catch (SqlException ex) {
Console.WriteLine(ex.Message);
}
finally {
thisConnection.Close();
Console.WriteLine("Connection Closed.");
}
}
}
Now, in the configuration file add a coneection string as follow
<configuration>
<connectionStrings>
<add name="ConnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyData\App_Data\Database.mdf;Integrated Security=True;User Instance=True"/><connectionStrings>
This technique has an advantage is say when your application is already deployed then you can change the connection string from the config file at any time without redefinig the code it's somewhat like the ini file
