0

I have the following code for connecting to my database on GoDaddy:

string connStr = "server=IP;" +
   "user=user_name;database=database_name;" +
   "port=3306;password=password;pooling=true;";

MySqlConnection conn = new MySqlConnection(connStr);

try
{
    Response.Write("Connecting to MySQL database...<br/>");
    conn.Open();

    string sql = "show tables";
    MySqlCommand cmd = new MySqlCommand(sql, conn);
    MySqlDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
        Response.Write(rdr[0] + "<br/>");
    }
    rdr.Close();
}

catch (Exception ex)
{
    Response.Write(ex.ToString());
}

conn.Close();
Response.Write("All done!");

This is the response:

Connecting to MySQL database... System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at MySql.Data.MySqlClient.MySqlConnection.AssertPermissions() at MySql.Data.MySqlClient.MySqlConnection.Open() at _Default.ConnectToDatabase() The action that failed was: InheritanceDemand The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The Zone of the assembly that failed was: MyComputerAll done!

What am I doing wrong?

BTW: If i run it in debugging mode on my computer, there's no problem. Only when I publish it to my server on GoDaddy.

mason
  • 31,774
  • 10
  • 77
  • 121
float
  • 371
  • 1
  • 6
  • 16
  • This isn't related to your error message, but you are not handling your database connections correctly. You need to take special care with anything that implements [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) so that you don't leak resources. – mason Mar 15 '17 at 13:14
  • Thank you for responding. I actually don't know what IDisposable is and how to handle database connections save. What do I have to change? – float Mar 15 '17 at 13:17
  • I figured you didn't know what IDisposable is. That's why I hyperlinked the word in my first comment to point to the documentation for it. – mason Mar 15 '17 at 13:19
  • I tried the solutions from: http://stackoverflow.com/questions/1484025/security-exception-using-mysql-and-entity-framework-on-godaddy. Didn't work either. – float Mar 15 '17 at 13:24
  • Really? Did you try setting the trust level to full as suggested? – mason Mar 15 '17 at 13:27
  • Yes I tried. Didnt work. – float Mar 15 '17 at 13:56

0 Answers0