locked
SqlConnection Class for multiple forms RRS feed

  • Question

  • Hello, I have a doubt regarding the correct way to solve my problem 

    I created a class to link to access a sql database. 

    The idea is to have an active connection for multiple forms.

    class LigacaoBD
        {

                public SqlConnection Ligacao = new SqlConnection();
                public bool servidor_ligado = false;

                public LigacaoBD()
                {

                }

                public LigacaoBD(string utilizador, string pwd)
                {

                    try
                    {

                        ligacao = "Server = RICARDO; Database = SEALIAH; User ID=" + utilizador + ";Password=" + pwd + ";";
                        Ligacao = new SqlConnection(ligacao);

                        resultado = Ligacao.Database;
                        servidor_ligado = true;
                        Ligacao.Open();
                        Ligacao.Close();
                    }
                    catch (Exception ex)
                    {
                        servidor_ligado = false;
                        resultado = ex.Message;
                    }

                }
            }

     I use this code in the first form for pass the active connection

      private void cmd_entrar_Click(object sender, EventArgs e)
            {

                try
                {
                    LigacaoBD cnn = new LigacaoBD(textUtilizador.Text, textPwd.Text);

                    if (cnn.servidor_ligado == true)
                    {
                        this.Hide();
                        FormPrincipal Form = new FormPrincipal();
                        Form.connect = cnn.Ligacao;
                        Form.status_connection = cnn.servidor_ligado;
                        Form.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Error in user");
                    }
                   
                 }
                catch (Exception ex1)
                {
                    MessageBox.Show( ex1.ToString(), "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    In the following forms I pass the connection using next code

     public partial class FormPrincipal : Form
        {
            public SqlConnection connect
            {
                get;
                set;
            }

            public bool status_connection
            {
                get;
                set;
            }

    }

               FormConsultarCliente Formulario = new FormConsultarCliente();
                Formulario.con = connect;
                Formulario.state_con = status_connection;
             

         There is a more efficient way of maintaining an active connection for all forms.

    Thanks

    Tuesday, January 21, 2014 4:23 PM

Answers

  • Hi Ricardo,

    You don't get any advantage, by the opposite, in doing this because ADO.NET manages that for you behind the scene and reuse the SQL connections by himself even when you close and destroy your SqlConnection. That's why the SqlConnection has an static method to close all the open connections so you can drop a database per example.

    So, the most recommend way to use the SqlConnection (and SqlDataReader) is inside of a "using" to avoid maintain connections opened just because you forget a close or even because an unexpected exception. That's why both of these classes are "IDisposable".

    Thanks

    Tuesday, January 21, 2014 10:53 PM

All replies

  • Put the properties in the database class as public properties. So you'll have them ready for you in any form.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Tuesday, January 21, 2014 4:56 PM
  • Hi Ricardo,

    You don't get any advantage, by the opposite, in doing this because ADO.NET manages that for you behind the scene and reuse the SQL connections by himself even when you close and destroy your SqlConnection. That's why the SqlConnection has an static method to close all the open connections so you can drop a database per example.

    So, the most recommend way to use the SqlConnection (and SqlDataReader) is inside of a "using" to avoid maintain connections opened just because you forget a close or even because an unexpected exception. That's why both of these classes are "IDisposable".

    Thanks

    Tuesday, January 21, 2014 10:53 PM
  • Hello,

         Create single instance class can slove your problem.

    -----------------------------------------
     Mark as answer if it helps to you
     Free .NET Barcode Generator & Scanner supporting over 40 kinds of 1D & 2D symbologies.
    Wednesday, January 22, 2014 3:27 AM
  • Hi Ricardo

    As Mr. Carlos mentioned, there is no point in keeping a global active connection. In fact it will mess up things at runtime. .NET has been designed to take away lot of developers headache. Otherwise there is no advantage of using .NET

    Wednesday, January 22, 2014 8:05 AM