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 のバックアップが出来ます
|
|
【PowerShell + C#の最新記事】
- nuget.exe CLI を使用してパッケージをダウンロードし、C# のソースコードで利用して PowerShell でビルドする
- PowerShell を使用して、C# のコンソールアプリ用のソースコードから exe を作成する( WebClient で wget.exe ) / ビルドせずに PowerShell で実行
- PowerShell で VisualStudio で作成した Form アプリケーションをビルドする( DataGridView に select 文の結果を表示する / MySQL )
- PowerShell で VisualStudio で作成した Form アプリケーションをビルドする( Form アプリケーションを テキストエディタのみで作成するテンプレート )
- TKMP + imap + C# + PowerShell : メールボックス(階層)の一覧表示
- PowerShell( 実質C# )を使用して、ファイルの分割を行う
- PowerShell( 実質C# )を使用して、MessageBox の応答でバッチファイルの処理を変化させる
- PowerShell で C# のソースコード(get_rec_mysql.cs) を使用して System.Data.Odbc で MySQL のデータを一覧表示( csv )






