Most efficient way to test SQL connection string availibility

I have this code down which I tried to make it Test SQL string connectivity, but I dont know how to handle the part with connection.Open = true would you please help me to solve this out? Thank you so much for your time.

  private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection("Data Source='" + textBox1.Text + "';Initial Catalog='" + textBox2.Text + "';User ID='" + textBox3.Text + "';Password='" + textBox4.Text + "'"))
            {
                try
                {
                    connection.Open();
                    if (connection.Open == true) // if connection.Open was successful
                    {
                        MessageBox.Show("You have been successfully connected to the database!");
                    }
                    else
                    {
                        MessageBox.Show("Connection failed.");
                    }
                }
                catch (SqlException) { }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Chyba v přihlášení: " + ex);
        }
        finally
        {

        }
    }

It says: “Cannot asign ‘open’ because it is a ‘methoud group’ ” I know that this code might be totaly bad, but I need to handle this somehow and have no idea what is the right way. Thank you.

  • Stored proc and program are still running even though browser is closed?
  • One-To-Many mapping fluent NHibernate
  • What is a SQL Server module?
  • How to convert a string to int SQL Server (Store Procedure)
  • How to make SQL Server 2012 delete expired records and check on a daily basis
  • Does TRANSACTION ISOLATIoN LEVEL SERIALIZABLE create READ lock
  • This is what is not actually working for not-opened connection:

    using (SqlConnection connection = new SqlConnection("Data Source='" + textBox1.Text + "';Initial Catalog='" + textBox2.Text + "';User ID='" + textBox3.Text + "';Password='" + textBox4.Text + "'"))
            {
    
                 connection.Open();
    
                if (connection.State == ConnectionState.Open)
                {
    
                    MessageBox.Show("Spojení s databázi problěhlo úspěšně.");
                }
                connection.Close();
                if (connection.State == ConnectionState.Closed)
                {
                    MessageBox.Show("Spojení selhalo");
                }
            }
    

  • How to Total Columns as a row
  • Unable to count the rows returned by a query containing OPTION(MAXRECURSION) as sub-query
  • Extracting WHERE clause to speed up query
  • Customized search with special characters
  • LIKE work-around in SQL (Performance issues)
  • Fill a Recursive Data Structure from a Self-Referential Database Table
  • 2 Solutions collect form web for “Most efficient way to test SQL connection string availibility”

    You’re using connection.Open = true as if it were a property.

    It’s a method: connection.Open()

    Use the ConnectionState enum to determine if the connection is open or not, eg:

    connection.State == ConnectionState.Open
    

    You need to check if it’s open by this code:

    if(connection.State == ConnectionState.Open)
    {
      ...
    }
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.