Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have this connection string code below;

C#
string conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();
            SqlConnection cn;
            cn = new SqlConnection(conn);
            cn.Open();


How can I create a Class/Method to be able to call just a string or variable whenever I need this connection strings on my code page?

I hope my question is clear? Thanks for helping in advance.

What I have tried:

C#
string conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();
            SqlConnection cn;
            cn = new SqlConnection(conn);
            cn.Open();
Posted
Updated 9-Apr-20 6:08am
v2
Comments
Richard MacCutchan 9-Apr-20 11:06am    
What is wrong with doing it this (correct) way?
Maciej Los 9-Apr-20 11:28am    
Good point!
Maciej Los 9-Apr-20 11:27am    
Why?
What you mean by: "I need this connection strings on my code page"? Can you be more specific?
Balogun Tunde 9-Apr-20 20:15pm    
Thank you all for your response, I really appreciate.
The code is working perfectly, but I don't want to be copying and pasting this code everytime I want to connect to database. Im new in ASP.NET, Im coming from PHP. In PHP, you just have this code on a separate page and be referencing to the page anytime you need db connection. So, I want to achieve that too in ASP.NET.
Maciej Los 10-Apr-20 2:21am    
You don't need to copy code which is responsible for connection. Take a look at solution#2 by F-ES Sitecore. I guess this is what you want.

C#
public static class DatabaseManager
{
    public static SqlConnection GetConnection()
    {
        return GetConnection("DefaultConnection");
    }

    public static SqlConnection GetConnection(string name)
    {
        string conn = ConfigurationManager.ConnectionStrings[name].ConnectionString.ToString();
        SqlConnection cn;
        cn = new SqlConnection(conn);
        cn.Open(); // It's not a good idea to open early, you should open right before it is needed

        return cn;
    }
}


Usage;

C#
SqlConnection con = DatabaseManager.GetConnection();
 
Share this answer
 
Comments
PIEBALDconsult 9-Apr-20 13:36pm    
I think he wanted to provide the connection name as a parameter.
F-ES Sitecore 10-Apr-20 6:02am    
You can do that too, look more closely at the code :)

SqlConnection con = DatabaseManager.GetConnection("YourConnectionNameHere");
Maciej Los 10-Apr-20 2:20am    
5!
This is probably overkill for what you want to do, but it does have advantages: Instance Storage - A Simple Way to Share Configuration Data among Applications[^]
 
Share this answer
 
Comments
Balogun Tunde 9-Apr-20 20:17pm    
Thank you for your response, I really appreciate.
The code is working perfectly, but I don't want to be copying and pasting this code everytime I want to connect to database. Im new in ASP.NET, Im coming from PHP. In PHP, you just have this code on a separate page and be referencing to the page anytime you need db connection. So, I want to achieve that too in ASP.NET.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900