使用Azure Powershell执行.sql文件 [英] Use Azure Powershell to execute a .sql file

查看:76
本文介绍了使用Azure Powershell执行.sql文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了从Azure Powershell运行SQL命令的示例,例如

I've seen examples of running a SQL command from Azure Powershell e.g.

$connectionString = "Data Source=MyDataSource;Initial Catalog=MyDB;User ID=user1;Password=pass1;Connection Timeout=90"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = "CREATE TABLE...." 
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$command.ExecuteNonQuery()
$connection.Close()

是否可以用对包含完整SQL脚本的外部文件的引用替换$ query?

Is there a way to replace the $query with a reference to an external file that contains an entire SQL script?

谢谢.

推荐答案

我又搜索了20分钟,发现了它.

20 more mins of searching and I found it.

$connectionString = "Data Source=MyDataSource;Initial Catalog=MyDB;User ID=user1;Password=pass1;Connection Timeout=90"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = [IO.File]::ReadAllText("C:\...\TestSQL.sql")
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$command.ExecuteNonQuery()
$connection.Close()

这篇关于使用Azure Powershell执行.sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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