我如何使用MySQL用户在.NET的MySqlCommand定义的变量? [英] How can I use a MySql User Defined Variable in a .NET MySqlCommand?

查看:382
本文介绍了我如何使用MySQL用户在.NET的MySqlCommand定义的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想执行下面的MySQL语句

 设置@a = 1; SELECT @a;
 

与MySql.Data.MySqlClient

  [System.Reflection.Assembly] :: LoadWithPartialName(MySql.Data)

$密码='输入mypassword'

$ SQL =设置@a = 1; SELECT @a;

$服务器='localhost'的
$端口= 3306
$数据库=测试
$ USER ='根'


$康恩=新的对象MySql.Data.MySqlClient.MySqlConnection
$的ConnectionString =服务器= $服务器,端口= $端口;数据库= $数据库; UID = $用户; PWD = $密码;允许以零日期时间= YES
$ conn.ConnectionString = $的ConnectionString
$ conn.Open()

$ CMD =新的对象MySql.Data.MySqlClient.MySqlCommand($的SQL,$康恩)
$ DS =新对象System.Data.DataSet中
$ DA =新对象MySql.Data.MySqlClient.MySqlDataAdapter($ CMD)
$ da.fill($ DS)
$ conn.close()
$ ds.tables [0]
 

我得到一个致命的错误。

当我更换$ SQL由两种

  $ SQL =SELECT DATABASE();
 

  $ SQL =SELECT 1;
 

我得到预期的结果。

我发现这个<一href="http://stackoverflow.com/questions/958953/is-it-possible-to-use-a-mysql-user-defined-variable-in-a-net-mysqlcommand">question,但它并没有解决我的问题。

我想端口SQLIse(的 SQLPSX项目的一部分)的MySQL版本MySQLIse。

我要处理什么简单有效的MySQL语句。

编辑:

我试图运行的Sakila-schema.sql文件的部分 mysql的演示数据库安装的运行由像

脚本
  

MySQL的SOURCE   C:/temp/sakila-db/sakila-schema.sql;

解决方案

我发现这个<一个解决方案href="http://blog.tjitjing.com/index.php/2009/05/mysqldatamysqlclientmysqlexception-parameter-id-must-be-defined.html">bolg

我要补充

 ;允许用户变量=真
 

在连接字符串

  $的ConnectionString =服务器= $服务器,端口= $端口;数据库= $数据库; UID = $用户; PWD = $密码;允许以零日期时间= YES,允许用户变量= TRUE
 

工作。我与6.3.6.0版本进行了测试。的MySql.Data。

I simply want to execute the following Mysql statement

SET @a = 1;SELECT @a;

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]

I get a fatal error.

When I replace $sql by either

$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.

I want to process any simple valid mysql statements.

EDIT:

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

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

解决方案

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.

这篇关于我如何使用MySQL用户在.NET的MySqlCommand定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆