创建 PowerShell 连接字符串
我一直在尝试创建一个 ConnnectionString ,它允许我使用 PowerShell 连接到本地数据库。下面是我的代码:
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=localhost;Database=test;Uid=<username here>;Pwd=<password here>;"
$conn.Open()
$sql = "SELECT EMP_STATUS FROM test_table"
$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
$rdr = $cmd.ExecuteReader()
while($rdr.Read())
{
$test = $rdr["EMP_STATUS"].ToString()
}
Write-Output $test
但是,我不知道我做错了什么,并且已经拉扯我的头发很长一段时间了。谁能帮我弄清楚我在 ConnectionString 中做错了什么?
谢谢大家!!
我意识到我的第一个问题是我有 MySQL 数据库,而不是 SQL 数据库。因此,我将不得不使用不同的方法进行连接。这正是我需要你帮助的地方!到目前为止,我已将代码修改如下:
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection
$connString = "server=localhost;port=3306;uid=<username here>;pwd=<password here> ;database=test;"
$conn.ConnectionString = $connString
$conn.Open()
$sql = "SELECT EMP_STATUS FROM test_table"
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand($sql,$conn)
$rdr = $cmd.ExecuteReader()
$test = @()
while($rdr.Read())
{
$test += ($rdr["EMP_STATUS"].ToString())
}
Write-Output $test
但是,这里还有几个问题: 1)如何使用MySQL .NET连接工具连接本地MySQL数据库? 2)这个PowerShell脚本应该保存在哪里? 3)我还需要做任何额外的改变吗?
非常感谢
I have been trying to create a ConnnectionString that will allow me to connect to my local database using PowerShell. Below is my code:
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=localhost;Database=test;Uid=<username here>;Pwd=<password here>;"
$conn.Open()
$sql = "SELECT EMP_STATUS FROM test_table"
$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
$rdr = $cmd.ExecuteReader()
while($rdr.Read())
{
$test = $rdr["EMP_STATUS"].ToString()
}
Write-Output $test
However, I have NO CLUE what I am doing wrong and have been pulling my hair out for quite some time. Can anyone help me figure out what I am doing wrong in the ConnectionString?
Thanks everyone!!
I realized that my first problem was that I have MySQL database, not SQL database. As a result, I will have to connect using a different method. This is exactly where I need your help!! So far I have modified my code as follows:
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection
$connString = "server=localhost;port=3306;uid=<username here>;pwd=<password here> ;database=test;"
$conn.ConnectionString = $connString
$conn.Open()
$sql = "SELECT EMP_STATUS FROM test_table"
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand($sql,$conn)
$rdr = $cmd.ExecuteReader()
$test = @()
while($rdr.Read())
{
$test += ($rdr["EMP_STATUS"].ToString())
}
Write-Output $test
However, here are a few more questions:
1) How do you use the MySQL .NET connection tool to connect to a local MySQL database?
2) Where should this PowerShell script be saved?
3) Are there any additional changes I should make?
Thanks so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
然后 $test 只给你在选择中找到的最后一个值!
要让 $test 包含 select 中的所有值,请更改代码,如下所示:
try this:
then $test give you only the last value found in the select!
To have $test containing all value from select change your code like this: