我是PowerShell的新手。在C#中有没有办法调用mysql.exe并使用Powershell执行查询。我想执行查询并将结果转换为csv文件。
.\mysql.exe -C -B --host=amazonaws -P 3306 --user=user --password=pass --database=MyDB --execute="SELECT * FROM MyTable LIMIT 5" | Out-File "C:\output.sql"
using (PowerShell PowerShellInstance = PowerShell.Create())
{
????
PowerShellInstance.Invoke();
}
由于
答案 0 :(得分:0)
当然使用C#连接到MySQL数据库它更容易,但是他们希望它有一个PowerShell脚本
private static void GenerateNewInputFile(
string server,
string user,
string password,
int port,
string database,
string query,
string mysqlLocation,
string outputFilePath)
{
using (var powershell = PowerShell.Create())
{
var sb = new StringBuilder();
sb.Append($"$server = \"{server}\"\n");
sb.Append($"$user = \"{user}\"\n");
sb.Append($"$password = \"{password}\"\n");
sb.Append($"$port = {port}\n");
sb.Append($"$db = \"{database}\"\n");
sb.Append($"$query = \"{query}\"\n");
sb.Append($"$mysql = \"{mysqlLocation}\"\n");
sb.Append("$params = \"-C\",\"-B\",\"-h$server\",\"-P$port\",\"-u$user\",\"-p$password\",$db,\"-e$query\"\n");
sb.Append($"& $mysql @params | Out-File \"{outputFilePath}\"\n");
powershell.AddScript(sb.ToString());
powershell.Invoke();
}
}