6

So after some tries I figured out I needed a driver for this. I've installed the Components from the links below. But I still can't find any SQL references when I try to add them? I'm wondering if anyone would know the reason for this? I just started with asp.net. I've found several other questions regarding code for connecting but I can't find anyone who've had trouble with the Connector/components before?

MYSQL connector http://dev.mysql.com/downloads/connector/net/5.0.html

Microsoft Data Access Components (MDAC) 2.8 http://www.microsoft.com/downloads/details.aspx?FamilyID=6c050fe3-c795-4b7d-b037-185d0506396c&displaylang=en

I am using Visual Studio.

2 Answers 2

16

Right click on References, then select "Add Reference". Browse and select Mysql.Data.dll.

The dll should be found in the installation directory of the connector you downloaded.

enter image description here

Finally, in code:

using MySql.Data.MySqlClient;

And, sample connection:

MySqlConnection connection = new MySqlConnection("Database=database_name;Data Source=server_domain_or_ip;User Id=mysql_user;Password=mysql_password");
connection.Open();

MySqlCommand command =  connection.CreateCommand();
command.CommandText = "select * from mytable";
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
  //reader.GetString(0)
  //reader["column_name"].ToString()
}
reader.Close();
Sign up to request clarification or add additional context in comments.

6 Comments

I deleted my last comment, I think I've found the problem. It might be the Host that only accept connections locally (the mysql is on my website and my program is on my computer). I'll test this code on another mysql database this evening.
if you are using 3rd party hosting, they by default accept connection to mysql from localhost only. Some provide you the option in the control panel to add some external IP's to accept incoming connection. If your pc does not have a fixed IP, then it may be a problem. Because, on serverside, you may have to allow incoming connection to mysql from all IP. Which is not good in regard of security. In this case, best option is to develop a webservice on server, which will access database. Then, from desktop app, get data via web service (and no direct db connection)
Also, you have to hardcode your mysql server password in your app, which anyone can get by simply decompiling the exe. Again, what if you need to change the mysql server password? Or database schema?
Before I went here I found a nice example where the connection string is saved in the web.config file (using asp.net) - This should do so that not everyone can get it unless I am mistaken? currently security is not my issue it's the connection. I contacted the place where i host my website and the database only accept connections locally. So now I'm looking for a new webhost :) Thank you for your answers they've been really helpful!
If your project is a website then it is absolutely fine :) I thought you were connecting to mysql from some desktop application.
|
0

You can browse to the DLL and add a reference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.