-1

I'm trying to retrieve data from database in Microsoft visual studio 2013 . I am totally lost whether I am already able to connect to the database or not and I am not sure how to retrieve data using c# as I am totally new to c#. I am also not sure where I should put the static void main method statement before or after connectDB() method.

private void connectDB()
{
   // server = "172.20.129.159";
    database = "eyetracker";
     server = "localhost";
    // uid = "ogamaaccess";
    // password = "ogama";
    uid = "root";
    password = "root";
    string connectionString;
    connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
    c = new MySqlConnection(connectionString);
    Console.WriteLine("Connected to database");


}

private bool OpenConnection()
{
    try
    {
        c.Open();
        Console.WriteLine("Connection Opened.");
        return true;

    }
    catch (MySqlException)
    { 
         return false;
    }
SPP
  • 21
  • 3
  • 9
  • have you written any code ? – Mainak Oct 29 '15 at 06:11
  • There are millions of tutorials, examples etc. available. Just type first six words from your question title into google search string - and you'll get it. – Andrey Korneyev Oct 29 '15 at 06:13
  • And you also should know which technology you want to use. There are many approaches on working with database and handling data access layer. – manman Oct 29 '15 at 06:18
  • "I am also not sure where I should put the static void main method statement before or after connectDB() method." There method order isn't important for the compiler, you can put it anywhere. – Jannik Jan 19 '16 at 07:17

1 Answers1

0

You need to install the MySql NET Connector that provides the appropriate bits to connect to MySQL database.

After installing the provider you need to add a reference to MySql.Data.Dll and add the appropriate using statement to your code

using MySql.Data.MySqlClient;

You also need to change your connection string which could be found in here.

Connection code should look something close to this:

private void Login() // login method
{
    string connectString = @"uid=<UserID>;password=<Password>;
                           server=<IPorDomainNameOfDatabase>;
                           database=<DatabaseNameOnServer>;";;


    using(MySqlConnection cnn = new MySqlConnection(connectString))
    {
        try
        {
            cnn.Open();
        }
        catch (Exception e)
        {
           .....
        }
    }
}

Full code could look something like this (command should be edited according to data you want to retrieve):

            MySqlConnection connect  = new MySqlConnection(connectString);
            MySqlCommand command = connect.CreateCommand();
            command.CommandText  = "Select <VALUE> from <TABLE> order by <ID> desc limit <0,1>;"; 
            //Command to get query needed value from DataBase
            connect.Open();
            MySqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                var result = reader.GetString(0);
            }

Note: I strongly suggest you to put connect.Open(); into TryCatch statment, since there are many things that can do wrong and your program will crash.