0

I'm new to C# and I'm trying to make this code work to test some commands in my database.

Fiddle here.

Full code here:

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public class Tutorial2
{
    public static void Main()
    {
        string connStr = "server=localhost;user=root;database=test;port=3306;password=root;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();

            string sql = "SELECT state_user_id FROM app_state ";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]+" -- "+rdr[1]);
            }
            rdr.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        conn.Close();
        Console.WriteLine("Done.");
    }
}

It doesn't work, I'm getting this error in the console:

Connecting to MySQL...
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 Tutorial2.Main() in d:\Windows\Temp\oup13in3.0.cs:line 16
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:
MyComputer
Done.

How can I fix that ?

Thanks

Community
  • 1
  • 1
kaycee
  • 901
  • 1
  • 9
  • 35
  • can you connect to the database with the username and password specified? Also, please post your code here and not on a third party site. – user1666620 Mar 04 '16 at 17:02
  • Updated the main post. Yes this is the correct login information... – kaycee Mar 04 '16 at 17:03
  • try the answers on this question http://stackoverflow.com/questions/32428/how-do-i-resolve-a-system-security-securityexception-with-custom-code-in-ssrs – user1666620 Mar 04 '16 at 17:05
  • What is happening when you use Integrated Security=SSPI; instead of using username and password? Getting error again? – Eray Balkanli Mar 04 '16 at 17:06
  • I've seen this post but as I'm working on Fiddle, I don't have a web.config file... – kaycee Mar 04 '16 at 17:07
  • something like: connectionstring = "Data Source=localhost; Initial Catalog=test; Integrated Security=SSPI;" ; – Eray Balkanli Mar 04 '16 at 17:08

0 Answers0