SQLの窓

2017年04月18日


csc.exe を使用して、コマンドプロンプトから C# 5 のアプリケーションをビルドする



32ビット用 : %windir%\Microsoft.NET\Framework\v4.0.30319
64ビット用 : %windir%\Microsoft.NET\Framework64\v4.0.30319

以下64ビット環境です。
※ Windows10 でも動作していますが、以下のメッセージが出力されます
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version.
csc.exe は、c# 用のコンパイラです。面倒な場所ではあるので、以下のスクリプトで簡単にプロンプトが作成できます
<JOB>
<RESOURCE id="commandList">
<![CDATA[
mode con: cols=120
set PATH=%windir%\Microsoft.NET\Framework64\v4.0.30319;%PATH%
prompt C#$G
]]>
</RESOURCE>

<OBJECT id="WshShell" progid="WScript.Shell" />
<OBJECT id="Fso" progid="Scripting.FileSystemObject" />

<SCRIPT language=VBScript>
' ***********************************************************
' 処理開始
' ***********************************************************

strPath = WScript.ScriptFullName
Set obj = Fso.GetFile( strPath )
Set obj = obj.ParentFolder
WshShell.CurrentDirectory = obj.Path

aData = Split( GetInline( "commandList" ), vbCrLf )
strCommand = "cmd.exe /k " & aData(0)
For I = 1 to Ubound( aData )
	strCommand = strCommand & "&" & aData(I)
Next
Call WshShell.Run( strCommand, 3 )

' ***********************************************************
' 関数
' ***********************************************************
Function GetInline( strName )

	GetInline = RegTrim( getResource( strName ) ) & vbCrLf

End Function
Function RegTrim( strValue )

	Dim regEx, str

	Set regEx = New RegExp
	regEx.IgnoreCase = True
	regEx.Pattern = "^[ \s]+"
	str = regEx.Replace( strValue, "" )
	regEx.Pattern = "[ \s]+$"
	RegTrim = regEx.Replace( str, "" )

End Function
</SCRIPT>
</JOB>

VBScript で記述されており、通常エクスプローラから実行すると最大化ウインドウでコマンドプロンプトが開くように作成してあります。

コマンドプロンプトからは、csc ソースコード.cs と入力するだけで基本的なビルドはすぐできます。

sample.cs
using System;
using System.IO;

// ********************************************************
// 実行
// ********************************************************
public class App
{

	public static void Main() {

		// PATH 環境変数取得
		String strEnv = Environment.GetEnvironmentVariable( "PATH" );

		// 配列定義
		String delimStr = ";";
		Char[] delimiter = delimStr.ToCharArray();

		String[] split = null;

		// トークン分割
		split = strEnv.Split( delimiter );

		// ソート
		Array.Sort( split, split.GetLowerBound(0), split.Length );

		// 書き込み
		try {
			StreamWriter OutFile = new StreamWriter( @".\result.txt", false );
			foreach (String strValue in split) {
				OutFile.WriteLine( strValue );
			}
			OutFile.Flush();
			OutFile.Close();
	
		}
		catch( Exception e ) {
			Console.WriteLine("エラーの内容 : {0}", e.ToString());
		}
	}

}

オプションは csc /? で表示されます。

mysql.cs( ODBC アクセス )
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.Odbc;

public class App
{

	// ********************************************************
	// MySQL / System.Data.Odbc
	// ********************************************************

	public static void Main()
	{
		string myConnectString = "Driver={MySQL ODBC 5.3 Unicode Driver};" + "SERVER=localhost;" + "DATABASE=lightbox;" + "UID=root;" + "PWD=password";

		OdbcConnection myCon = new OdbcConnection();
		myCon.ConnectionString = myConnectString;
		myCon.Open();

		string myQuery = "SELECT 社員マスタ.*,DATE_FORMAT(生年月日,'%Y-%m-%d') as 誕生日" + " from 社員マスタ";
		OdbcCommand myCommand = new OdbcCommand();
		myCommand.CommandText = myQuery;
		myCommand.Connection = myCon;

		OdbcDataReader myReader = default(OdbcDataReader);
		myReader = myCommand.ExecuteReader();


		while (myReader.Read()) {
			// 文字列
			Console.Write(GetValue(myReader, "社員コード") + " : ");
			Console.Write(GetValue(myReader, "氏名") + " : ");

			// 整数
			Console.Write(GetValue(myReader, "給与") + " : ");

			// 日付
			Console.Write(GetValue(myReader, "作成日") + " : ");
			Console.Write(GetValue(myReader, "更新日") + " : ");
			Console.Write(GetValue(myReader, "生年月日") + " : ");
			Console.Write(GetValue(myReader, "誕生日"));
			Console.WriteLine();

		}

		myReader.Close();

		myQuery = "update 社員マスタ set 生年月日 = '1982/01/01'" + " where 社員コード = '0002'";
		Execute(myCon, myQuery);

		myCon.Close();

		myReader.Dispose();
		myCon.Dispose();

	}

	// ********************************************************
	// 列データ取得
	// ********************************************************
	public static string GetValue(OdbcDataReader odr, string strName)
	{

		string ret = "";
		int fld = 0;

		fld = odr.GetOrdinal(strName);
		if (odr.IsDBNull(fld)) {
			ret = "";
		} else {
			ret = odr.GetValue(fld).ToString();
		}

		return ret;

	}

	// ********************************************************
	// 更新処理
	// ********************************************************
	public static int Execute(OdbcConnection cn, string SQL)
	{

		int ret = 0;
		OdbcCommand execCommand = new OdbcCommand();

		execCommand.CommandText = SQL;
		execCommand.Connection = cn;
		try {
			ret = execCommand.ExecuteNonQuery();
		} catch (Exception ex) {
			Console.WriteLine(ex.Message);
		}
		execCommand.Dispose();

		return ret;

	}

}




タグ:C#
【VS(C#)の最新記事】
posted by lightbox at 2017-04-18 22:08 | VS(C#) | このブログの読者になる | 更新情報をチェックする
container 終わり



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

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