I am trying to connect to a database "database1" using C#, however, everytime that I try to connect I get this error:

Unable to connect to any of the specified MySQL hosts.

This is the code that I am using to connect to database1

string connString = "Data Source=localhost; Database=Database1.mdf; User ID=root; Password=root";
MySqlConnection conn = new MySqlConnection(connString);

Can anyone recommend anything / give me some help?

Thank you!

7 Years
Discussion Span
Last Post by Momerath

hmm, you have to specify the full path to the database, like:

string connString = "Data Source=localhost; Database=C:\MyFolder\MyProject\Database1.mdf; User ID=root; Password=root";

If not, you can use instead of a full path, implicit path: "|DataDirectory|\.Database1.mdf"



Thank you for your reply; it helped a lot..

The only problem being is that I need to hand in this assignment to my tutor, and, when he's marking it - He won't have the ability to see the database, if I specify the directy to be associated with me (If you see what I mean?) Is there anyway around not putting the directory?

Also, would it be better (in your opionion if I hosted a database online?) Thanks :)


If you want that your tutor (btw, who is this?) sees it, its a must to host it online. Which DB server do you use now? MySql Express?
Becuase for using the dataBase so everyone can access to it (so online), you will have to put this database to some other sql server, like Sql server 2008 (this one I have).
And then the connection string is different, there is needed:
- database name
- ip of the server
- username (of the user who create db)
- password (of the user who create db)

Hope it helps,

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.