如何在。net MySqlCommand中使用MySql用户定义的变量?

时间:2022-09-26 06:49:13

I simply want to execute the following Mysql statement

我只想执行下面的Mysql语句。

SET @a = 1;SELECT @a;

with MySql.Data.MySqlClient

与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]

I get a fatal error.

我犯了一个致命的错误。

When I replace $sql by either

当我用这两种方法替换$sql时

$sql = "SELECT DATABASE();"

or

$sql = "SELECT 1;"

I get the expected result.

我得到了预期的结果。

I found this question, but it doesn't solve my problem.

我找到了这个问题,但它不能解决我的问题。

I'm trying to port SQLIse (a part of the SQLPSX project ) to the MySQL version MySQLIse.

我正在尝试将SQLIse (SQLPSX项目的一部分)移植到MySQL版本的MySQLIse。

I want to process any simple valid mysql statements.

我想处理任何简单有效的mysql语句。

EDIT:

编辑:

I was trying to run parts of the sakila-schema.sql the mysql demo database install script which runs by something like

我试图运行部分sakila-schema。sql mysql演示数据库安装脚本,其运行方式类似

mysql> SOURCE C:/temp/sakila-db/sakila-schema.sql;

mysql >源C:/ temp / sakila-db / sakila-schema.sql;

1 个解决方案

#1


41  

I found the solution in this bolg

我找到了解

I have to add

我已经添加

;Allow User Variables=True

to the connection string

的连接字符串

$connectionstring =  "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes;Allow User Variables=True"

works. I tested it with version 6.3.6.0. of MySql.Data.

的工作原理。我用6.3.6.0版测试了它。MySql.Data。

#1


41  

I found the solution in this bolg

我找到了解

I have to add

我已经添加

;Allow User Variables=True

to the connection string

的连接字符串

$connectionstring =  "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes;Allow User Variables=True"

works. I tested it with version 6.3.6.0. of MySql.Data.

的工作原理。我用6.3.6.0版测试了它。MySql.Data。