Hi,
I have the connection string in web.config file and want to encrypt it and how to create a class to connect to database? Should it be static,private or public? Please advice
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Nov 16, 2011, 4:22 AM
Use Public for class and static for property that returns connection strings to you. like
Pravin MorePosted Nov 16, 2011, 2:56 AM
HI Nethra,
yes you can make it abstract but then you need to implement method of abstract class in every class where you inherit that abstract class.
see, normally we use abstract class where processing of derived class for method of base changes in every derived class......
so in our case ,if you want to use different database connection for in diff scinario....
i guess that, you are going to declare more that connection string in web.config and will use perticular in perticular class....
if so,
then i have 2 solution
1] use abstract class inwhich declare method getconnection (use same class i given in my previous reply just make it abstract)
and in every class where you want to open connection just inherit that abstract class n override method in that method code you can change line that set connection string i.e
dbConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["commonconnection"].ToString());
here you can give any name you want instead of "commonconnection"
2] second way is somwhat simple
dont use abstract class use simple class and in that class use simple method which takes 1 parameter i.e Connectioname n you will pass name of connection you given in web.config.....
here is example.....
public class Dbconnection
{
public SqlConnection getcon(string strcon)
{
SqlConnection dbConn = null;
try
{
dbConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["+strcon+"].ToString());
}
catch (Exception e1)
{
}
return dbConn;
}
}
and wherever you want any connection to open just create object of above class and call that method with parameter as value you set in web.config...
suppose, this is web config file
if you want open con1
then Dbconnection db1=new Dbconnection();
db1.getcon("con1");
if you want open con2
then Dbconnection db1=new Dbconnection();
db1.getcon("con2");
like that........
so nethra ,i recomend you 2nd solution
Thanks ,
Pravin.
Nethra R SPosted Nov 15, 2011, 10:49 PM
Satyapriya NayakPosted Nov 8, 2011, 12:59 AM
connection.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
///
/// Summary description for connection
///
public class connection
{
static string my_connection1 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
static SqlConnection my_connection = null;
public static SqlConnection get()
{
if (my_connection == null)
{
my_connection = new SqlConnection(my_connection1);
my_connection.Open();
}
return my_connection;
}
}
How to call the connection.cs class in a given program
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
public void bindgrid()
{
SqlCommand com = new SqlCommand("select * from employee", connection.get());
SqlDataAdapter sqlda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sqlda.SelectCommand = com;
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";
GridView1.DataBind();
}
}
Thanks
If this post helps you mark it as answer
Kunal NaikPosted Nov 8, 2011, 12:39 AM
Hi,
Make a class as public for encrypt the connection string, in that use,
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ToString());
Now you make another class take it as public and to use this class to connect to db. In that get connection string from above class.
Pravin MorePosted Nov 8, 2011, 12:22 AM
here is code of class for database connection.....you can follow it.
public class Dbconnection
{
public SqlConnection getcon()
{
SqlConnection dbConn = null;
try
{
dbConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["commonconnection"].ToString());
}
catch (Exception e1)
{
}
return dbConn;
}
}
Thanks, i hope it will help you.
Pravin.