mysql - 为什么execute-mysql命令无法连接到服务器并运行字符串

标签 mysql powershell

我正在尝试登录服务器和数据库并让命令运行连接字符串。所有发生的事情都是当我运行下面发布的命令时。

function Execute-MySqlcommand {param( [string]$Server,    #the host of the SQL server
                                    [string]$Database1,   #the name of the database
                                    [System.Data.MySqlclient.MySqlcommand]$Command)  #the command to execute (name of stored command)

 $mysqlConnection = new-object System.Data.MySqlclient.MySqlConnection
 $MySqlConnection.ConnectionString = "DROP_VIEW DATABASE.BTXADDR;DROP_VIEW DATABASE.BTXSUPB;CREATE_VIEW DATABASE.BTXADDR FOR DATABASE1.DATABASE1S2.BTXADDR;CREATE_VIEW DATABASE.BTXSUPB FOR DATABASE1.DATABASE1S3.BTXSUPB"
 $MySqlConnection.ConnectionString = "TRUNCATE TABLE DATABASE1.DATABASE1S2.BTXADDR;TRUNCATE TABLE DATABASE1.DATABASE1S3.BTXSUPB; INSERT INTO DATABASE1.DATABASE1S3.BTXSUPB SELECT * FROM DATABASE1.DATABASE1S2.BTXSUPB; select count(*) from DATABASE1.DATABASE.BTXADDR; select count(*) from DATABASE1S.DATABASE.BTXADDR; select count(*) from DATABASE1.DATABASE.BTXSURB; select count(*) from DATABASE1S.DATABASE.BTXSUPB;"

 $Command.CommandType = 1 # 1 is the 'Text' command type
 $Command.Connection = $mysqlConnection

 $mysqlConnection.Open()
 $Result = $Command.ExecuteNonQuery()
 $mysqlConnection.Close()

 if ($Result -gt 0) {return $True} else {return $False}

}

运行命令时发生的所有情况都表明我在脚本中编写的内容没有错误,然后退出脚本。但我正在尝试登录服务器并运行脚本命令。

我自己登录到服务器来检查该命令是否实际运行,并且它显示了之前存在的原始 View 。

最佳答案

您的连接字符串不应包含您要运行的 SQL。如果您使用 Windows 域帐户进行连接,则为

$mysqlConnection.ConnectionString = "Server=$Server;Database=$Database1;Integrated Security=True"

对于命令

$command.CommandText = 'Drop View or whatever SQL you want to run'
$returnVal = $command.ExecuteNonQuery()

关于mysql - 为什么execute-mysql命令无法连接到服务器并运行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329831/

相关文章:

azure - 无法使用 mcr.microsoft.com/windows/servercore :ltsc when building from azure cloudshell (powershell)

mysql - 数据库中 "categories"数据如何结构化?

php - 使用php将XML数据插入mysql

c# - SQL Server 条件连接

PHP MYSQL 更新查询不起作用没有错误

php - 使用 PHP 返回行的问题

powershell - 将负数设置为零Powershell

windows - 在远程机器上创建私有(private)消息队列

c++ - 当前步骤 : Building Tasks. json 文件

电源外壳 7 : using ampersand (&) in string literals