SQLの窓

2018年10月21日


nuget.exe + SMO + PowerShell + C# : テーブルの CREATE TABLE 文 を取得

nuget.exe Windows x86 Commandline のダウンロードページ

ダウンロードする SMO のバージョン確認

▼ SMO のダウンロード
nuget install Microsoft.SqlServer.SqlManagementObjects -Version 140.17283.0
▼ PowerShell 実行用のバッチファイル
powershell -NoProfile -ExecutionPolicy Unrestricted .\create_sql.ps1 sa ""
create_sql.ps1
# バッチファイルからの引数
$user = $Args[0]
$pass = $Args[1]

$code = @"
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

public class MyClass {

	public static void create_sql() {

		// BOM なしのテキスト用
		// https://docs.microsoft.com/ja-jp/dotnet/api/system.text.utf8encoding.-ctor?view=netframework-4.7.2
		UTF8Encoding UTF8N_Enc = new UTF8Encoding(false);

		// false は上書き
		// https://docs.microsoft.com/ja-jp/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2
		StreamWriter WriteFile = new StreamWriter("sqlexpress.sql", false, UTF8N_Enc);

		// サーバー : Microsoft.SqlServer
		Server srv;
		// インスタンス
		srv = new Server();

		// サーバーインスタンスの情報
		srv.ConnectionContext.AutoDisconnectMode = AutoDisconnectMode.NoAutoDisconnect;
		srv.ConnectionContext.LoginSecure = false;

		// PowerShell の文字列の埋め込み
		srv.ConnectionContext.Login = "${user}";
		srv.ConnectionContext.Password = "${pass}";

		// 接続
		srv.ConnectionContext.Connect();

		// バージョンの表示
		Console.WriteLine(srv.Information.Version);

		// データベース選択
		Database myDb = srv.Databases["lightbox"];

		// テーブル一覧
		foreach (Table myTable in myDb.Tables) {

			// テーブルに関するスクリプト一覧
			StringCollection tableScripts = myTable.Script();

			// スクリプトの書き込み
			foreach (string script in tableScripts) {
				// テキストファイル書き込み
				WriteFile.WriteLine(script);
			}
		}

		// 出力テキストファイルの後処理
		WriteFile.Close();
		WriteFile.Dispose();

		// 接続解除
		srv.ConnectionContext.Disconnect();

	}
}
"@

# 共通のパスを作成
$path = "C:\user\nuget\Microsoft.SqlServer.SqlManagementObjects.140.17283.0\lib\net40"

# 配列定義
$dll = @(1..5)

# 配列にライブラリファイル名を代入
$dll[0] = "Microsoft.SqlServer.ConnectionInfo.dll"
$dll[1] = "Microsoft.SqlServer.Smo.dll"
$dll[2] = "Microsoft.SqlServer.Management.Sdk.Sfc.dll"
$dll[3] = "Microsoft.SqlServer.SmoExtended.dll"
$dll[4] = "Microsoft.SqlServer.SqlEnum.dll"

# PowerShell の単純変数の埋め込みと配列変数の埋め込み
Add-Type -Path `
	( `
	"${path}\$($dll[0])", `
	"${path}\$($dll[1])", `
	"${path}\$($dll[2])", `
	"${path}\$($dll[3])", `
	"${path}\$($dll[4])" `
	)

Add-Type -Language CSharp -TypeDefinition $code -ReferencedAssemblies `
	( `
	"${path}\$($dll[0])", `
	"${path}\$($dll[1])", `
	"${path}\$($dll[2])", `
	"${path}\$($dll[3])", `
	"${path}\$($dll[4])" `
	)

# 実行
[MyClass]::create_sql()

関連する記事

C# : SQLServer( SQLExpress ) の SMO を使用してテーブルの CREATE TABLE 文 を取得する

C# : VB.net : SQLExpress(SQLServer) : SQL-DMO と同等の SMO によるバックアップ
上のリンク先のコードを使用すれば、PowerShell で SQLServer のバックアップが出来ます
posted by lightbox at 2018-10-21 10:12 | PowerShell + C# | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり