Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,
i have winform c# visual studio 2012
i want to implement "singleton design pattern"
in fact i don't want to open a new connection every time i need to request the database.
before going to the database, i want to check if the connection is already open or not
if it opens, so no need to create a new instance
otherwise, create.
And in every page of my app i just want to call my singleton whenever i need data from database without having to open or close again a DB connection

What I have tried:

namespace GNMS
{
    public class Godaddy
    {
        //singleton has a reference to itself
        private static Godaddy dbconec;
        private Godaddy() { }//private constructor so that it cannot be instantiated outside this class

        //grabs instance of singleton pattern
        public static Godaddy GetInstance()
        {
            if (dbconec == null) //check if  an instance has been created else  can create a new instance
            {
                dbconec = new Godaddy();
            }
            return dbconec;
        }

        public string connectDB() //Database connection 
        {

            try
            {

                //create a string connection
                string strconnection;
                strconnection = "Server=x.x.x.x; Database =xxx; Uid = xxx; Password =xxx;CharSet=utf8; Connect Timeout=60;";
                //initialize the connection to DB
                MySqlConnection conn = new MySqlConnection(strconnection);

                conn.Open();

                return strconnection;

                //conn.Close();

            }

            catch (Exception ex)
            {

                return ex.Message;
            }

        }

        ////
    }
}
Posted
Updated 12-Apr-16 10:28am
v2
Comments
PIEBALDconsult 12-Apr-16 16:09pm    
That is a bad idea. While you may need only one connection to one database right now, you may very well need multiple connections some time down the road.
And you should _always_ open and close the connection as you use it and not leave it open for the duration of the application.

The standard "how to" for Singletons in C# -- C# in Depth: Implementing the Singleton Pattern[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-16 16:47pm    
5ed. Despite your "never a valid reason" claim. — see my comment to Solution 1.
—SA
In reality (except embedded applications maybe, where size is critical), there is no reason to implement a single connection...ADO.NET uses a connection pool solution to minimize time of connection...You can rely on it (I have a web application installed at over 100 customer's site, handling thousands of request - all for data - in any minute...It relies on ASP.NET pool)...

Connection Pooling[^]
SQL Server Connection Pooling (ADO.NET)[^]
ADO.NET Connection Pooling at a Glance[^]
 
Share this answer
 
v2
Comments
PIEBALDconsult 12-Apr-16 16:37pm    
There is _never_ a valid reason to implement the Singleton Pattern. And most examples (e.g. database access and logs) are bogus.
Armel_Djient 12-Apr-16 17:23pm    
according to you, how can manage the access to my database such that my winform app will not go down?
note: i have mysql phpmyadmin
PIEBALDconsult 12-Apr-16 17:40pm    
First, you shouldn't put data access code directly in the Form; you should have a Data Access Layer in its own class.
Typically, for WinForms, I instantiate the connection and pass it to the DAL.
Then I pass a reference to the DAL into the constructor for the Form; the Form will then hold a reference to the DAL and use it as it requires.

See also:
http://www.codeproject.com/Articles/753789/Simplified-Database-Access-via-ADO-NET-Interfaces
http://www.codeproject.com/Articles/19990/DatabaseAccessor
Armel_Djient 14-Apr-16 3:29am    
i got your point and i think youu are right. Subdivise my app in 3 layers(presentation, business logic and data access layer) will be benefict for me.
thanks for ur reply
Sergey Alexandrovich Kryukov 12-Apr-16 16:46pm    
5ed.

About "never a valid reason" claimed by PIEBALDconsult: this statement makes sense; one can really develop without singletons, but, in practice, this claim is some kind of puristic exaggeration.

—SA

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