2012年9月6日 星期四

[C#] 統計arraylist內各個項目出現次數


//註: arDif是存放資料的ArrayList, 我們要統計裡面存放的資料, 項目以及其出現次數

               Dictionary<double, int> result = new Dictionary<double, int>();
                foreach (int d in arDif)
                {
                    if (result.ContainsKey(d))
                    {
                        result[d] = result[d] + 1;
                    }
                    else
                    {
                        result.Add(d, 1);
                    }
                }  


//show資料

            foreach (KeyValuePair<double, int> item in result)
            {
                textBox9.Text += string.Format("{0} : {1}"+Environment.NewLine, item.Key, item.Value);
            }

[C#] ArrayList轉Array


static void Main(string[] args)
{
    {
        ArrayList Lists = new ArrayList();
        Lists.Add("a");
        Lists.Add("b");
        Lists.Add("c");
        string[] s = (string[])Lists.ToArray(typeof(string));
        foreach (string item in s) {
                    Console.WriteLine(item);
                }
    }
    {
        ArrayList Lists = new ArrayList();
        Lists.Add(1);
        Lists.Add(2);
        Lists.Add(3);
        int[] i = (int[])Lists.ToArray(Type.GetType("System.Int32"));
        foreach (int item in i) {
                    Console.WriteLine(item.ToString());
                }
    }
}

2012年7月16日 星期一

[PHP] 插入資料至MSSQL2008

由於PHP 5.3.6已沒有支援MSSQL, 因此上網找方法 (參詳 JT-client-Side)

說是要去下載微軟製做的 SQLServerDriverForPHP11

它上面給的連結好像已經失效

因此客倌請參詳 老洪的IT筆記本

裡面有說明和載點

下載下來之後, 把適用於自己的版本的dll檔放到/php5/ext資料夾裡面,

我發現還是不行一樣出現錯誤, 然後就在試著複製一份到system32裡面,


複製完之後請到/php5/php.ini 加入

[php_SQLSRV]

extension = php_sqlsrv_53_ts.dll (按各自版本不同而有異)

extension = php_pdo_sqlsrv_53_ts.dll (按各自版本不同而有異)

extension = php_pdo.dll


重啟apache, 開瀏覽器打http://localhost:port/phpinfo.php

就看到下面這個, 這就表示php和MSSQL可以連線囉














寫段程式碼測試看看, 結果是OK的~!
<?php
header('Content-type: text/html; charset=utf-8');
$serverName = "127.0.0.1";
$uid = "sa";
$pwd= "密碼";
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"Topology","CharacterSet"=>"UTF-8");

$connect = sqlsrv_connect($serverName, $connectionInfo);

if($connect == false)
{
echo "Unable to connect.<br/>";
die(print_r(sqlsrv_errors(),true));
}else
{
echo " Connected!<br />";
}

$sqlcmd = "insert into node_info values(2,3,5,6)";
$stmt = sqlsrv_query($connect, $sqlcmd);
/*
$row = sqlsrv_fetch_array($stmt);
echo $row[0]."<br />";
echo $row[1]."<br />";
echo $row[2]."<br />";
echo $row[3]."<br />";
*/
sqlsrv_free_stmt($stmt);
sqlsrv_close($connect);

?>


2012年6月18日 星期一

[C#] for迴圈執行, 即時動作

當執行迴圈時, 想要讓迴圈執行到哪就顯示什麼,
例如下面的例子:  
想要執行到0的時候就馬上print 0、執行到1時就print 1
但是通常都會等到迴圈跑完才會一次print出來


for(int i =0; i < n; i++)
{
           ........
           textBox1.Text += "i";
}


因此上網找到了兩種解決方法就可以搞定, 如下:

for(int i =0; i < n; i++)
{
           ........
           textBox1.Text += "i";
           Application.DoEvents();      
          //或者 textBox1.Refresh();
}

2012年6月8日 星期五

[C#] Split以多字元切割方式

Example:   abcd++efg++yei
切割字串時, 若以++這兩個符號作為分別切割的辨識項, 單純用str.split只能以一字元做為切割辨識項目String test = strs.Split(',');

若要以多字元切割方法如下:
String[] split_text = Text.Split(new String[] {"字串"}, StringSplitOptions.RemoveEmptyEntries);