如何在 .NET MySqlCommand 中使用 MySql 用户定义变量? [英] How can I use a MySql User Defined Variable in a .NET MySqlCommand?
问题描述
我只是想执行下面的Mysql语句
I simply want to execute the following Mysql statement
SET @a = 1;SELECT @a;
使用 MySql.Data.MySqlClient
with MySql.Data.MySqlClient
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$password = 'mypassword'
$sql = "SET @a = 1;SELECT @a;"
$server = 'localhost'
$port = 3306
$database = 'test'
$User = 'root'
$conn=new-object MySql.Data.MySqlClient.MySqlConnection
$connectionstring = "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes"
$conn.ConnectionString = $connectionstring
$conn.Open()
$cmd=new-object MySql.Data.MySqlClient.MySqlCommand($sql,$conn)
$ds=New-Object system.Data.DataSet
$da=New-Object MySql.Data.MySqlClient.MySqlDataAdapter($cmd)
$da.fill($ds)
$conn.close()
$ds.tables[0]
我收到一个致命错误.
当我用任一替换 $sql 时
When I replace $sql by either
$sql = "SELECT DATABASE();"
或
$sql = "SELECT 1;"
我得到了预期的结果.
我发现了这个 问题,但它不能解决我的问题.
I found this question, but it doesn't solve my problem.
我正在尝试将 SQLIse(SQLPSX 项目的一部分)移植到 MySQL 版本的 MySQLIse.
I'm trying to port SQLIse (a part of the SQLPSX project ) to the MySQL version MySQLIse.
我想处理任何简单有效的 mysql 语句.
I want to process any simple valid mysql statements.
我试图运行 sakila-schema.sql mysql 演示数据库安装脚本,由类似
I was trying to run parts of the sakila-schema.sql the mysql demo database install script which runs by something like
mysql> 源C:/temp/sakila-db/sakila-schema.sql;
mysql> SOURCE C:/temp/sakila-db/sakila-schema.sql;
推荐答案
我在这个博客
我必须补充
;Allow User Variables=True
到连接字符串:
$connectionstring = "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes;Allow User Variables=True"
有效.我用 6.3.6.0 版对其进行了测试.MySql.Data
.
works. I tested it with version 6.3.6.0. of MySql.Data
.
这篇关于如何在 .NET MySqlCommand 中使用 MySql 用户定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!