Connection String
Include the following lines in the configuration section of web.config file.
- <appSettings>
- <add key="con" value="Server = My Server name; Initial Catalog= My Database name; User ID = abcd; Password = *****;"/>
- </appSettings>
Connection Class File
Add a class file by right clicking on the project -- Add -- Class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- namespace WebApplication1 {
- public class code {
- SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
- public DataSet select(string s) {
- DataSet ds = new DataSet();
- SqlDataAdapter da = new SqlDataAdapter(s, con);
- da.Fill(ds);
- return ds;
- }
- public int insert(string s) {
- int i;
- SqlCommand cmd = new SqlCommand(s, con);
- con.Open();
- i = cmd.ExecuteNonQuery();
- con.Close();
- return i;
- }
- }
- }
Note: In the webform, just create an object for the connection class file, thus reducing the complexity of the code........!!