我只是想执行下面的Mysql语句
SET @a = 1;SELECT @a;
与 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]
我收到 fatal error 。
当我用其中之一替换 $sql 时
$sql = "SELECT DATABASE();"
或
$sql = "SELECT 1;"
我得到了预期的结果。
我找到了这个question ,但这并不能解决我的问题。
我正在尝试将 SQLIse(SQLPSX project 的一部分)移植到 MySQL 版本 MySQLIse。
我想处理任何简单有效的 mysql 语句。
编辑:
我试图运行 sakila-schema.sql 的部分内容mysql 演示数据库安装脚本,其运行方式类似于
mysql> SOURCE C:/temp/sakila-db/sakila-schema.sql;
最佳答案
我在这个blog中找到了解决方案
我必须添加
;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
。
关于.net - 如何在 .NET MySqlCommand 中使用 MySql 用户定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077494/