views:

715

answers:

2

Is it possible to set connection collation within MySql connection string and how, since there's a default setting on the server that's used for new connections.

Two things I can't do:

  1. Can't call SET COLLATION_CONNECTION after I open a connection, because I'm using Entity Framework that does all the calls for me not entirely true as you may see in the edit
  2. Can't change server default connection collation because of other databases and their respected applications that use them.

All I'd like to specify is a certain connection string parameter in my web.config file like:

"User id=dbuser;Password=dbpass;Host=dbserver;Database=testung;Collation=utf8_general_ci"

but Collation setting/variable isn't recognised.

Technologies used

  • Asp.net MVC 2
  • IIS 7
  • Entity Framework 1
  • DevArt dotConnect MySql connector
  • MySql 5.1

EDIT 1

I've tried this code as @Devart suggested but to no avail:

partial void OnContextCreated()
{
    System.Data.Common.DbCommand command = this.Connection.CreateCommand();
    command.CommandText = "set collation_connection = utf8_slovenian_ci;";
    command.CommandType = System.Data.CommandType.Text;
    this.Connection.Open();
    command.ExecuteNonQuery();
    // this.Connection.Close();
}
A: 

According to the ever handy connection strings site, the keyword should be charset.

Mark Wilkins
No it doesn't work. This is just charset setting, not collation.
Robert Koritnik
Okay. Sorry about that. I had looked at the documentation about their collations and character sets, and it appeared to me that they were combining them into a single setting.
Mark Wilkins
+1  A: 

We recommend you to implement the OnContextCreated partial method.
You have access to the store connection in it and you can execute ADO.NET command "SET COLLATION = ..." using this connection.

Devart
Good idea. I've tried doing this, please check my **EDIT 1** up on the question.
Robert Koritnik
Just replace the "System.Data.Common.DbCommand command = this.Connection.CreateCommand();" line with the "System.Data.Common.DbCommand command = (this.Connection as EntityConnection).StoreConnection.CreateCommand();" one.
Devart
@Devart: This works only if I don't close the connection afterwards. If I close it (as in my upper code) I get collation error. So I'm wondering whether EF closes it afterwards? In this case I wouldn'0t worry too much. But if it doesn't... I would have to close it myself. Afterwards if at all possible.
Robert Koritnik
Closing the connection results in the collation loss (the session is lost too in this case).EF will close the connection after the query is executed. The connection will be closed on the dispose of the ObjectContext anyway.
Devart