2014年9月4日木曜日

SQLで複数レコードInsertちゃん!

Oracle

Insert All
Into Table名(Column1, Column2, ....)
Values (val1, val2, ....)
Into Table名(Column1, Column2, ....)
Values (val1, val2, ....)

select * from dual


SQLSERVER

Insert Into Table名(column1, column2, ....)
Values(val1, val2, ....)
,Values(val1, val2, ....)

SQLServerのがすきだなInsert

2014年8月6日水曜日

SQLServer2012でmdbをアタッチしたらアクセスできません、の件

タイトルどおり。5:アクセスできません とエラーが出る。

そーなのかーと思い管理者権限で実行。変わらず。
調べてみるとアクセス許可がいるそう(フォルダのプロパティ>セキュリティ>対象ユーザーの変更許可)

Windowsじゃあまり意識することないからぱっとエラーメッセージからアクセス権限ってのは気づかなかった

2014年7月3日木曜日

プロセス終了検知

プロセスが終了するまで次の処理を行わない


using System.Diagnostics;

class Project
{
    //メイン
    public static void Main(string[] args)
    {
        //プロセスが終了するまで次のプロセスを行わない
        string processName = "notepad";

        if (IsStopedProcess(processName))
        {
            System.Diagnostics.Debug.WriteLine("notepad終了");
        }
    }

    /// 
    /// プロセス終了検知
    /// プロセスが終了するまでループし続ける
    /// 
    ///     /// 
    public static bool IsStopedProcess(string processName)
    {
        foreach (var process in Process.GetProcessesByName(processName))
        {
            //プロセスが終了したらTrue
            while (!process.HasExited)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
        return true;
    }
}


プロセスが終了するまで待つ

この場合メモ帳が閉じられるまで無限ループし続ける
プロセスがなければProcess.GetProcessesByNameで拾えず抜けるし、拾えたらHasExitedでプロセスが閉じるまで監視し続ける

このプロセスが終わってから次の処理へ、としたい場合に使う

2014年6月18日水曜日

セマフォ!Semaphore!スレッドの実行数指定

セマフォの初期実行数0にハマったのでここに記す。
0だとReleaseしない限りWaitOneでtrue返らないなんて。
0からカウントアップして最大実効数まで使えると思ってたよ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace Project4
{
    class Program
    {
        //セマフォ
        //スレッドの同時実効数を設定。この場合初期実行数0, 最大実効数2
        static Semaphore _sem = new Semaphore(0, 2);

        static void Main(string[] args)
        {
            //労働者5人を働かせる
            for (var i = 0; i < 5; i++)
            {
                Emploee emp = new Emploee(i + 1);
                emp.SetSemaphore = _sem;
                emp.IsWorking = true;

                Thread t = new Thread(new ThreadStart(emp.working));

                t.Start();
            }

            Thread.Sleep(500);
            
            //初期実行数0なのでReleaseしないとWaitOneでシグナルを取得できない
            _sem.Release(2);
        }

    }   

    /// 
    /// 従業員。働け
    /// 
    class Emploee
    {
        #region member
        /// セマフォ
        private Semaphore _sem;

        /// 労働者番号
        private int _number;
        #endregion

        #region Property
        /// セマフォ
        public Semaphore SetSemaphore
        {
            set { this._sem = value; }
        }

        /// 労働中?
        public bool IsWorking { get; set; }
        #endregion

        public Emploee(int number)
        {
            this.IsWorking = false;
            this._number = number;
        }

        public void working()
        {
            //働いてもらう
            while (this.IsWorking) {

                //指定ミリ秒数毎に監視。
                //セマフォ.WaitOneで実行数 -1。実行数が0ならWaitOneでfalseが返る
                if (this._sem.WaitOne(300))
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("わたしは{0}番の労働者です。労働しています。", this._number));
                    Thread.Sleep(200);

                    //働いたらあがっていい
                    this.IsWorking = false;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("わたしは{0}番の労働者です。待機中です。", this._number));
                }
            }

            //開放するとセマフォの実行数 + 1
            this._sem.Release();
        }
    }
}


実行結果

わたしは2番の労働者です。待機中です。
わたしは4番の労働者です。待機中です。
わたしは5番の労働者です。待機中です。
わたしは1番の労働者です。待機中です。
わたしは3番の労働者です。待機中です。
わたしは2番の労働者です。労働しています。
わたしは3番の労働者です。労働しています。
わたしは4番の労働者です。待機中です。
わたしは1番の労働者です。待機中です。
わたしは5番の労働者です。待機中です。
わたしは5番の労働者です。労働しています。
わたしは1番の労働者です。労働しています。
わたしは4番の労働者です。労働しています。

2014年6月9日月曜日

ActiveDirectoryてなにさ

WindowsServerの機能でネットワーク上ユーザーやPCやグループを管理

接続認証とかをやるのがDomainControllerちゃん!

IT用語
http://e-words.jp/w/Active20Directory.html

@IT 第1回 Active Directoryとは何か?
http://www.atmarkit.co.jp/ait/articles/0209/12/news003.html



2014年6月1日日曜日

オブジェクトの初期化構文 - C#ショートプログラミング -

オブジェクトに初期値を与えたい


OLD
class Example
    {
        internal string A, B, C;
    }
    class Program
    {
        static void Main(string[] args)
        {
            var sample = new Example();
            sample.A = "{0} {1}";
            sample.B = "This is B!";
            sample.C = "This is C!";
            System.Diagnostics.Debug.WriteLine(sample.A, sample.B, sample.C);
        }
    }

NEW
//C#2.0
    class Example
    {
        internal string A, B, C;
    }
    class Program
    {
        static void Main(string[] args)
        {
            var sample = new Example()
            {
                A = "{0} {1}",
                B = "This is B!",
                C = "This is C!",
            };
            System.Diagnostics.Debug.WriteLine(sample.A, sample.B, sample.C);
        }
    }

Result

This is B! This is C!

・初期化専用構文
・インスタンスを明示しなくていいのでメンバー数が多いほど効果大

引用元:
C#ショートプログラミング





条件演算子とnull合体演算子 - C#ショートプログラミング -

string型の内容を出力したいが、nullの場合は"(null)"と出力する


OLD1
class Program
    {
        static void Main(string[] args)
        {
            string a = "We are the Hello World";
            if (a == null)
            {
                System.Diagnostics.Debug.WriteLine("(null)");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(a);
            }

        }
    }

OLD2
class Program
    {
        static void Main(string[] args)
        {
            string a = "We are the Hello World";
            System.Diagnostics.Debug.WriteLine(a == null ? "(null)" : a);
        }
    }

NEW
class Program
    {
        static void Main(string[] args)
        {
            string a = "We are the Hello World";
            System.Diagnostics.Debug.WriteLine(a ?? "(null)");
        }
    }

Result

We are the Hello World

・?? はnull合体演算子という。オペランドが null 以外の場合、左オペランドを返し、それ以外の場合は右オペランドを返す。
・C#にしかないようなので汎用性は低い。

引用元:
C#ショートプログラミング