Entity Framework : Set MySQL custom environment variables - mysql

I have an issue with Entity Framework 5.0. I'm working with Silverlight 5 and MySQL 5.6 too.
I need to set an environment MySQL variable before each connexion to the MySQL server.
E.g
SET #my_var = 'test';
Under Mysql I don't have any issues.
The following raises an EntityFrameworkException (syntax error near '#').
this.ObjectContext.CreateQuery<object>(" SET #my_var = 'test' ");
OR
this.ObjectContext.CreateQuery<object>(" CALL set_my_var('test') ");
This last method raises a MySQLException saying that a DataReader is already open and need to be closed.
this.ObjectContext.ExecuteStoreQuery<object>(" CALL set_my_var('test') ", null);
I also tried to set a MySQL system environment (no '#') with the same result every time.
Any help will be much appreciated !
Thank you.

I tried so many things that I misspelled my variable in my code.
So the following finaly worked : ctx.ExecuteStoreCommand("SET #my_var = 'test'");
I decided to leave the instruction in the method Initialize of my domain service. This method is inherited of the LinqToEntitiesDomainService class.
But you need to set Allow User Variables=True in your MySQL connection string
(ref : Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?)
You simply need to use a recent version of the MySQL Connector because older versions use the '#' mark to define SQL parameters so it could conflict with custom variables. Now it uses the '?' mark : http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html
My library was already up to date (6.6.5).
Thank you for the help !

Since your statement is not a query (i.e. does not return any result) you should use ExecuteStoreCommand. Something like this should work:
ctx.ExecuteStoreCommand("SET #my_var = 'test'")

Related

User defined variables using SET is not working

I am using node 5.0.0 along with the node module mysql2 - 1.0.0-rc.11. It is working perfectly fine except when I try to use the user defined variables.
Example :
SET #user := 123456;
SELECT * FROM users WHERE user = #user;
The above simple query is throwing syntax error at my end. I am sure that, the syntax is a valid one. So I am wondering what may be the cause for this issue?
Doesn't the node module mysql2 - 1.0.0-rc.11 have support for SET yet? I have scanned the [document1, document2] thoroughly, I couldn't able to find the syntax support list. Can anyone help me to understand what's going wrong with this?
Following up on Andrey Sidorov's comment, you will need to have multipleStatements: true in the options object you pass to createConnection(), createPool(), etc. This is false by default for security reasons, so I don't recommend using it in production code.
https://github.com/mysqljs/mysql#multiple-statement-queries

How to check current strings in MYSQL_HISTIGNORE?

In mysql 5.6 a new parameter histignore is introduced to avoid writing of history file for some particular keywords.
I run the mysql shell by setting two string in histignore parameter as:
./bin/mysql --histignore="*UPDATE*:*DELETE*"
I want to know how to check this variable current values ?
Whats the command to check present values of this variable ?
I got the solution:
We need to set the variable as:
export MYSQL_HISTIGNORE="*INSERT*"
and we can check via command :
printenv MYSQL_HISTIGNORE

How to set up the FOUND_ROWS flag?

I'm using Entity Framework 5, MySQL 5.6 and MySQLConnector 6.6.5.
I would like to set up the FOUND_ROWS flag in my Entity Framework connection string.
I tried the following configuration.
...
server=localhost;user id=root;option=2
..
According to the MySQL documentation the value '2' equals FOUND_ROWS parameter.
But it throws an InvalidOperationException saying that the Keyword is not supported (name parameter : option).
How am I supposed to do?
try
UseAffectedRows=false
reference.
UseAffectedRows
When true, the connection reports changed rows instead of found rows.
This option was added in Connector/Net version 5.2.6.

Database connection string and collation

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

How can I get the database name from a Perl MySQL DBI handle?

I've connected to a MySQL database using Perl DBI. I would like to find out which database I'm connected to.
I don't think I can use:
$dbh->{Name}
because I call USE new_database and $dbh->{Name} only reports the database that I initially connected to.
Is there any trick or do I need to keep track of the database name?
Try just executing the query
select DATABASE();
From what I could find, the DBH has access to the DSN that you initially connected with, but not after you made the change. (There's probably a better way to switch databases.)
$dbh->{Name} returns the db name from your db handle.
If you connected to another db after connected with your dbh, using mysql query "USE db_name", and you did not setup a new perl DBI db handle, of course, $dbh->{Name} will return the first you previously connected to... It's not spontaneic generation.
So to get the connected db name once the db handle is set up - for DBI mysql:
sub get_dbname {
my ($dbh) = #_;
my $connected_db = $dbh->{name};
$connected_db =~ s/^dbname=([^;].*);host.*$/$1/;
return $connected_db;
}
You can ask mysql:
($dbname) = (each %{$dbh->selectrow_hashref("show tables")}) =~ /^Tables_in_(.*)/;
Update: obviously select DATABASE() is a better way to do it :)
When you create a connection object it is for a certain database. In DBI's case anyway. I I don't believe doing the SQL USE database_name will affect your connection instance at all. Maybe there is a select_db (My DBI is rusty) function for the connection object or you'll have to create a new connection to the new database for the connection instance to properly report it.
FWIW - probably not much - DBD::Informix keeps track of the current database, which can change if you do operations such as CREATE DATABASE. The $dbh->{Name} attribute is specified by the DBI spec as the name used when the handle is established. Consequently, there is an Informix-specific attribute $dbh->{ix_DatabaseName} that provides the actual current database name. See: perldoc DBD::Informix.
You could consider requesting the maintainer(s) of DBD::MySQL add a similar attribute.