Database connection string and collation

六眼飞鱼酱① 提交于 2019-12-01 08:53:27

问题


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();
}

回答1:


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.




回答2:


If anyone else stumbles over this problem or wants to issue a command when opening a connection: The answer regarding OnContextCreated does no longer work as the method does no longer exist/is no longer supported.

An alternative, which I use for executing SET NAMES <character set used by the database> is to append ;initialization command=\"SET NAMES '" + CharSet + "';\" to your connection string. According to Devart's documentation this also works for PostgreSQL, MSSQL and Oracle

This property can also be set inside EntityDeveloper when accessing the properties of the database connection and clicking on the Advanced button.



来源:https://stackoverflow.com/questions/2132530/database-connection-string-and-collation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!