Sql connection works with MySql.Data but not with Dapper/IDbConnection with path not found error

40 Views Asked by At

I am trying to change my SQL connections from using MySql.Data to using Dapper to retrieve my data from a MySQL database running on a VPS. I have no problems connecting with the following connection class with MySql.Data

class Connection
{
    MySqlConnection conn;
    static string host = "Fake Name";
    static string database = "Fake Database";
    static string userDB = "Fake User";
    static string password = "Fake Password";
    
    public bool Open()
    {
        try
        {
            strProvider = "Server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
            conn = new MySqlConnection(strProvider);
            conn.Open();
            return true;
        }
        catch (Exception er)
        {
            MessageBox.Show("Connection Error ! " + er.Message, "Information");
        }
        return false;
    }
    public void Close()
    {
        conn.Close();
        conn.Dispose();
    }
    public DataSet ExecuteDataSet(string sql)
    {
        try
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            da.Fill(ds, "result");
            return ds;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return null;
    }
    public MySqlDataReader ExecuteReader(string sql)
    {
        try
        {
            MySqlDataReader reader;
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            reader = cmd.ExecuteReader();
            return reader;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return null;
    }
    public int ExecuteNonQuery(string sql)
    {
        try
        {
            int affected;
            MySqlTransaction mytransaction = conn.BeginTransaction();
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            affected = cmd.ExecuteNonQuery();
            mytransaction.Commit();
            return affected;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return -1;
    }
}

However I'm trying to switch to Dapper so I can securely manage queries. I'm using the code below

DataAccess db = new DataAccess();
bookingList = db.GetBookings(BookingCalendar.SelectionStart.ToString("yyyy-MM-dd"));



public class DataAccess
{
    public List<Booking> GetBookings(string date) 
    {           
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.connectionString("Fake Name")))
        {
            return connection.Query<Booking>($"select * from Bookings WHERE Date ='{date}'").ToList();
        }
    }
}



public static class Helper
{
    public static string connectionString(string name)
    {
        return ConfigurationManager.ConnectionStrings[name].ConnectionString;
    }
}



<connectionStrings>
  <add name="Fake Name" connectionString="Server=Fake IP; Database=Fake Database; User Id=Fake User; Password=Fake Password providerName=System.Data.SqlClient"/>
</connectionStrings>

but on trying to open the connection I get a path not found error after 15 seconds. I'm not sure why this is happening as these two connection strings are identical. Any help with this would be much appreciated

1

There are 1 best solutions below

0
jmcilhinney On BEST ANSWER

Of course it doesn't work. You're using SqlClient in the second code snippet, which is specific to SQL Server. If you want to connect to MySQL then you still need to use a MySQL provider.