2 Replies - 977 Views - Last Post: 10 October 2010 - 05:08 PM Rate Topic: -----

#1 Tangela  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-October 10

can't connect to mysql database

Posted 09 October 2010 - 12:44 PM

I am trying to connect to my database I made in mysql workbench and I keep getting this error:
Unknown database 'sample'

I do not believe it has to do with my code i just think it has to do with mysql workbench. I am not sure if my server for mysql is running. But when I check test connection it tell me that its ok and i don't get errors. I also ran the MySQL Server Instance Config Wizard and i got no problems for it. Here is my code:

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace Connect_to_MySQL_Database
{
    class Program
    {
        static void Main(string[] args)
        {
            string config = "Server=localhost;Port=3306;Database=sample;Uid=root;password=root;";
            MySqlConnection con = new MySqlConnection(config);
            MySqlCommand command = con.CreateCommand();
            Console.Write("Hello World");
            command.CommandText = "Insert into user (username,password) values('felice','roller')";
            con.Open();
            command.ExecuteNonQuery();
            con.Close();
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: can't connect to mysql database

#2 Mitja Bonca  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 82
  • Joined: 07-May 09

Re: can't connect to mysql database

Posted 10 October 2010 - 01:03 AM

You code is wrong. Change it to the code that will look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Okt10DBAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = @"Server=localhost;Port=3306;Database=sample;Uid=root;password=root;";
            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                string queryString = String.Format(@"Insert into user (username,password) values(@userName, @password)");
                using (SqlCommand cmd = new SqlCommand(queryString, sqlConn))
                {
                    cmd.Parameters.Add("@userName", SqlDbType.VarChar, 50).Value = "felice";
                    cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = "roller";
                    sqlConn.Open();
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch { }
                    finally
                    {
                        sqlConn.Close();
                    }
                }
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 Tangela  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-October 10

Re: can't connect to mysql database

Posted 10 October 2010 - 05:08 PM

Hey I tried your code and I got a to the open function and I got a new error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1