所以我试图连接到localhost数据库。我可能没有正确创建用户。我在phpmyadmin>用户帐户中创建了用户,其中包含以下信息:
这是我的代码:
Try
{
$Server = "localhost"
$Database = "myDatabase"
$uid ="myUsername"
$pwd = "myPassword"
$SQLConnection = New-ObjectSystem.Data.SQLClient.SQLConnection
$SQLConnection.ConnectionString ="server=$Server;database=$Database;Integrated Security=True"
$SQLConnection.Open()
}
catch
{
[System.Windows.Forms.MessageBox]::Show("Failed to connect SQL Server:")
}
在第8行我也试过
$SQLConnection.ConnectionString ="server=$Server;database=$Database;Integrated Security=False;uid=$uid;pwd=$pwd"
和..
$SQLConnection.ConnectionString ="server=$Server;database=$Database;uid=$uid;pwd=$pwd"
答案 0 :(得分:0)
您正在尝试连接到MSSQL Server而不是MySQL。 您的连接应该像这样初始化:
$SQLConnection = New-Object MySql.Data.MySqlClient.MySqlConnection
答案 1 :(得分:0)
首先,您在第四点提到 MySql ,其中您的连接对象代表MSSQL。
如果您要连接到 MSSQL ,那么您在声明连接字符串时会出现语法错误。下面提到:
而非:
$Server = "localhost"
$Database = "myDatabase"
$uid ="myUsername"
$pwd = "myPassword"
$SQLConnection = New-ObjectSystem.Data.SQLClient.SQLConnection
$SQLConnection.ConnectionString ="server=$Server;database=$Database;Integrated Security=True"
$SQLConnection.Open()
这样做:
$Server = "localhost"
$Database = "myDatabase"
$uid ="myUsername"
$pwd = "myPassword"
$SQLConnection = New-Object System.Data.SQLClient.SQLConnection
$SQLConnection.ConnectionString ="server=$Server;uid=$uid; pwd=$pwd;database=$Database;Integrated Security=False"
$SQLConnection.Open()
如果您使用 Mysql ,则必须安装连接器,然后使用如下:
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$connectionString = "server=$server;uid=$uid;pwd=$pwd;database=$database;"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$sql = "show tables"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$table = New-Object System.Data.DataTable
$recordCount = $dataAdapter.Fill($table)
echo $table