Reputation: 201
I have an AWS EC2 instance running. I've tried to make a connection to my MySQL like this in C#
MySqlConnection l_DBConn = new MySqlConnection();
l_DBConn.ConnectionString = "Server=my-ec2.compute-1.amazonaws.com;Port=3306;Database=mydatabase;uid=root;password=mypassword;port=3306;charset=utf8"
MySqlCommand command = l_DBConn.CreateCommand();
command.CommandText = "select * from users";
MySqlDataReader Reader = command.ExecuteReader();
The error received when I run the last line is "Connection must be valid and open.". I still can connect to my instance using Filezilla,Putty and MySQL WorkBench also(with SSH). And I'm using MySql.Data.MySqlClient in my code. Anyone has any idea about this? Is this because my instance does not allow remote access or a problem with my connection string?
Thank you in advance, Wayne.
Edit: The problem is solved by adding l_DBConn.Open() and add my IP to the server using RANT ALL PRIVILEGES ON . TO root@'hostname' IDENTIFIED BY 'root-password'. Thanks!
Upvotes: 1
Views: 1666
Reputation: 18832
Some ideas:
Upvotes: 1
Reputation: 6578
Try calling l_DBConn.open()
before executing the reader. The error implies that the connection is not open since you haven't explicitely opened it
Upvotes: 1