Hello,

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);
conn.Open();

Can anyone recommend anything / give me some help?

Thank you!

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"

Hey,

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,
Mitja

if you put it in the same directory as the executable then you don't need the path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.