3

I'm attempting to create a method that I can call on any form in order to establish a mysql connection. This is the code I have

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace micromini
{
    public class dbcon
    {
        public void dbconnector
        {
            string strConnection = "host=50.87.144.250; database=twhalen_storage; username=twhalen_software; password=Karatom14!;";
            MySqlConnection conSQL = new MySqlConnection(strConnection);
            try
            {
                conSQL.Open();
            }
            catch (Exception ex)
            {
                string exstring = ex.ToString();
                MessageBox.Show(exstring);
            }
            MessageBox.Show("Welcome!");
        }
    }
}

The error I get is A get or set accessor expected. Any ideas?

crthompson
  • 15,653
  • 6
  • 58
  • 80
Twhalen
  • 63
  • 1
  • 5
  • 1
    I'm not entirely sure what you're trying to accomplish with that method but don't do it. [Read this](http://stackoverflow.com/a/9707060/4068) for a reason why (the answer is about SQL Server but the concept applies to database connections in general). – Austin Salonen Dec 03 '13 at 21:31

3 Answers3

9

You missed the parentheses () for the parameters after the name of the method, which is an obligatory part of the signature, so the compiler try to analyze your method as a property.

mcserep
  • 3,231
  • 21
  • 36
8

Add the () after the method name

 public void dbconnector()

This is required to differentiate a method from a property that requires the get/set syntax

However, this code opens the connection in a local variable and doesn't return it so it is basically useless. Not to mention that you don't close and dispose the connection.

In this way you rely on the Garbage Collector to dispose the variable and free the connection.
Used too many times in a row, this code could lead to the Too many open connections error

Steve
  • 213,761
  • 22
  • 232
  • 286
3

Without the paranthesis (), you have declared a Property not a method and a Property requires get and set accesors. Put () at the name of your method.

public void dbconnector()