2009年2月25日 星期三

資料庫連線字串大全

在網路上偶然看到以下這個網站:

ConnectionStrings.com - Forgot that connection string? Get it here!

大概想的到的資料庫都在裡面了......(當然,想不到的也有一大堆)

以後忘記連線字串怎麼寫就可以連進去參考囉!

當然,也可以透過 ConnectionStringBuilder 類別來產生......

2009年2月20日 星期五

[JavaScript] expando 屬性的定義與使用

最近在讀 Learning Jquery 時,看見 JavaScript expando,是個蠻有意思的東西,可以自行擴充 JavaScript object 的屬性。

而由於這些屬性是放置於 memory 中,因此使用起來速度較快,不過也要記得釋放,免得出現 memory leak 的問題。

在網路上查詢到以下的範例(內容有稍作修改),說明 JavaScript expando 的使用方式:
<html>
<head>
<script type="text/javascript">
<!--
var myObj = new Object();
alert(myObj.toString()); //出現 "[object Object]"

//設定 expando 屬性
myObj.myExpando = "Hello Expando";
myObj.toString = function(){ //override toString() method
return this.myExpando;
}
alert(myObj.toString()); //出現 "Hello Expando"

//設定 expando 屬性
myObj.reportTime = function(){
alert(new Date());
}
myObj.reportTime(); //出現目前的日期與時間

//刪除 expando 屬性
delete myObj.reportTime;

//再一次嘗試呼叫已經刪除的 expando 屬性
try {
myObj.reportTime();
} catch(e) {
alert(e.message); //出現 "myObj.reportTime is not a function"
}
-->
</script>
</head>
<body>
</body>
</html>



參考資料

2009年2月16日 星期一

[SQL Server] 減少 Transaction Log 所佔的硬碟容量

今天同事在問,之前也沒做過,所以上網找了一下資料,發現以下兩個不錯的連結說明:

Brad's MIS Note: 交易紀錄檔 Transaction Log 管理

數據庫日常維護(參考)

2009年2月12日 星期四

[jQuery] jQuery + AJAX

AJAX

AJAX,這個技術是透過 JavaScript 中的 XMLHttpRequest 物件去呼叫後端 server code 並動態改變前端的網頁,讓網頁不需要 refresh。

AJAX 的技術並不複雜,大概掌握幾個重點即可:
  1. XMLHttpRequest 物件將資料傳遞到後端 server 的方式,可分為 POST GET

  2. 從 server 回傳的資料可以是 plain text 或是 XML

  3. 前端網頁 DOM 的處理........(這其實跟 AJAX 已經沒有太大關係了)



jQuery 與 AJAX 的搭配


直接寫 pure XMLHttpRequest 的程式碼真的太累了,光是 cross browser 的問題就可以搞死人了......不過還好 jQuery 都幫大家處理好啦!

以下用的範例程式來說明:(demo)

ajax01.html
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
//取得 button 物件並註冊 click 事件
$("#btnAJAX").click(function(){
//取得使用者輸入的值
var intValue = $("#txtValue").val();

/* 以 POST 的方式送出 request
傳入參數 : value
參數值 = intValue
callback function 為 function(result){...} */

$.post("server01.php", {value : intValue}, function(result){
$("#spnMain").html("server 端回傳結果 = " + result);
});
});
});
-->
</script>
</head>
<body>
請輸入整數值,server 會計算其平方值並回傳:<p />
<input type="text" id="txtValue" /> <input type="button" id="btnAJAX" value="從 server 端取得結果" /><p />
<span id="spnMain"></span>
</body>
</html>

server01.php
<?php
if(isset($_POST["value"]))
echo $_POST["value"] * $_POST["value"];
else
echo "0";
?>

從上面的範例可以看出,透過 jQuery 物件的 post() 方法就可以直接享受到 AJAX 的好處囉! 當然,也可以用 get() 的方式來達到 AJAX 的效果。

此外,callback function 的部份比較需要注意的就是參數的傳遞了,在 jQuery 中可以允許使用一般的方式或是 context 的方式傳入參數,以下用範例說明:(demo)

ajax02.html
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
//測試函式
function testFunc(intVal) {
alert("final value is " + (intVal * 2 + 10));
} //end function


$(document).ready(function(){
//取得 button 物件並註冊 click 事件
$("#btnAJAX").click(function(){
//取得使用者輸入的值
var intValue = $("#txtValue").val();

/* 以 POST 的方式送出 request
傳入參數 : value
參數值 = intValue
callback function 為 function(result){...} */

$.post("server.php", {value : intValue}, function(result){
//直接呼叫 function
testFunc(result);

//使用 jQuery context 的方式呼叫 function
testFunc.apply(this, [result]);
});
});
});
-->
</script>
</head>
<body>
請輸入整數值,server 會計算其平方值並回傳:<p />
<input type="text" id="txtValue" /> <input type="button" id="btnAJAX" value="從 server 端取得結果" /><p />
<span id="spnMain"></span>
</body>
</html>


參考資料

[jQuery] jQuery 初體驗

第一支 jQuery 程式

很久之前就聽過 jQuery 的大名啦!

所以來試試看這個 JavaScript Framwork 到底厲害之處在哪裡..........

之前在查詢 JavaScript framework 的時候就有大概瞭解到,jQuery 的強項在於 DOM 的處理,據用過的人說,jQuery 處理 DOM 可是又快又簡單,是否真的有這麼神奇呢?

官網 tutorial 中的第一隻程式就有一些 DOM 的簡單應用:
<html>
<head>

<script type="text/javascript" src="js/jquery-1.3.1.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){
/* 預設按下超連結都會執行以下的效果 */
$("a").click(function(event){
alert("Thanks for visiting!");

//透過 event 的 preventDefault() 方法,停止原本要做的動作(網頁導向指定頁面)
event.preventDefault();

//將此 object 隱藏(有慢慢隱藏的效果)
$(this).hide("slow");
});

//幫每一個 hyperlink 都加上 css 效果(對應到上方程式中定義的粗體)
$("a").addClass("test");

/* 由於 jQuery 的 function 都會回傳原本的 query object,因此可以將程式串在一起執行
因此以下程式執行的工作為:
1、按下超連結時,搜尋 class 名稱���否為 "clickme",若���,執行以下工作:
1.1 顯示 "Thanks for visiting!" 訊息 (上面已經定義過的動作會照樣進行)
1.2 開始慢慢消失
1.3 顯示 "you are leaving the site." 訊息
1.4 由於顯示訊息的關係(花了點時間),會讓超連結一瞬間消失(其實它���慢慢消失的)
2、使用 end() 方法,回到開始進行 query 的 document object(原本��� hyperlink object)
3、按下超連結時,搜尋 class 名稱���否為 "hideme",若���,執行以下工作:
3.1 顯示 "Thanks for visiting!" 訊息 (上面已經定義過的動作會照樣進行)
3.2 開始慢慢消失(這裡不寫 hide 也行,因為上面已經用過)
3.3 回傳 false
4、最後又使用 end() 方法,回到開始進行 query 的 document object
*/

$("a").filter(".clickme").click(function(){
alert("you are leaving the site.");
}).end().filter(".hideme").click(function(){
$(this).hide();
return false;
}).end();
});
-->
</script>

<style type="text/css">
a.test { font-weight: bold; }
</style>

</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<p />
<a href="http://google.com/" class="clickme">I give a message when you leave</a>
<p />
<a href="http://yahoo.com/" class="hideme">Click me to hide!</a>
<p />
<a href="http://microsoft.com">I'm a normal link</a>
</body>
</html>



Event Regist
<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
alert("document is ready!");

/* $ 符號代表的意義為 jQuery class
$("a") 屬於 jQuery selector
並註冊了 click 的事件(事件的註冊會累計執行) */

$("a").click(function(){
alert("Hello World!");
});
/* 透過此種方式進行 event regist
就不用在每個 element 中撰寫類似「<a href="" onclick="alert('Hello world')">Link</a>」的語法了
這樣最大的優點是:「HTML、JavaScript、CSS 都可以徹底的分離了!」 */

-->
</script>
</head>
<body>
<a href="">Link</a>
</body>
</html>
使用 selector $("元素名稱") 選擇特定的 element,並進行 event regist。 (範例中註冊的是 click 事件)

透過 jQuery 的 event regist 方式,就可以作到將 HTML、JavaScript、CSS 三個徹底分離,讓後續管理更簡單方便。



Selector 的應用
<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
//尋找 ID 為 orderedlist 的 element,並增加 css 效果
$("#orderedlist").addClass("red");

//尋找 orderedlist 的 li element,並增加 css 效果
$("#orderedlist > li").addClass("blue");

//尋找 orderlist 中最後一個 li element,並註冊 hover 事件,指定發生的 css 效果
$("#orderedlist li:last").hover(function(){
$(this).addClass("green");
}, function(){
$(this).removeClass("green");
});

/* 尋找 ID 為 orderedlist 的 element
並在其所有的 li element 加上字串
而「$("#orderedlist").find("li")」的效果等同「$("#orderedlist > li")」*/

$("#orderedlist").find("li").each(function(i){
$(this).append(" BAM! " + i);
});
});
-->
</script>

</head>
<body>
<ol id="orderedlist">
<li>First element</li>
<li>Second element</li>
<li>Third element</li>
</ol>
</body>
</html>
  1. 使用 css + xpath 結合的方式尋找 element,例如範例中的「$("#orderedlist > li")」以及「$("#orderedlist li:last")

  2. 使用 each 在每個 element 後方在另外加上字串

<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
//尋找不包含 ul 的 li element,並指定 css 效果
$("li").not(":has(ul)").css("border", "1px solid black");
});
-->
</script>

</head>
<body>
<ol id="orderedlist">
<li>First element</li>
<li>Second element</li>
<li>Third element</li>
</ol>

<ol id="orderedlist2">
<li>First element, second list</li>
<li>Second element, second list</li>
<li>Third element, second list</li>
<li>Li with child ul
<ul>
<li>Child One</li>
<li>child two</li>
</ul>
</li>
</ol>
</body>
</html>
  1. 透過 not(expr):has(selector) 的搭配,取得不包含 ul 的 li element

  2. 使用 css(name, value) 指定 css 效果

<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
$("a[name]").css("background-color", "#bbb");
});
-->
</script>

</head>
<body>
<a href="">no name attribute</a>
<p />
<a name="test" href="">has name attribute</a>
</body>
</html>
透過 element[attribute_name] 的方式取得擁有特定 attribute 的元素

<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
/* 一連串的動作,說明如下:
1、尋找 ID 為 FAQ 的 element
2、尋找該 element 下的 dd element,並將其隱藏
3、使用 end() 回到 faq element 狀態
4、繼續尋找 dt element,並進行 event regist
5、設定 click 後,會讓 dt element 的下一個 element 進行 slideToggle() 的動作 */

$("#faq").find('dd').hide().end().find('dt').click(function(){
$(this).next().slideToggle();
});
//使用 end() 的優點在於 $("#faq") 僅會發生一次 DOM select
});
-->
</script>

</head>
<body>
<h3>Bird FAQ - click the questions to show the answers</h3>
<dl id="faq">
<dt>What shouldn't I do to the bird?</dt>
<dd>Never use oils or lotions which contain oils on your bird. They gunk up the feathers,
and ruin their insulating properties. This means a chilled bird. Never wait out a cat bite--those
require immediate veterinary attention--a bird can die within two days because a cat'
s mouth is so
filthy and full of bacteria. Don't bother with over-the-counter medication. It really doesn't work,
and in some cases, may upset the delicate bacterial balance in the bird's body, or even worsen the
situation. Never try to treat a fracture at home.</dd>

<dt>My bird is healthy. I don'
t need to go to a vet, do I?</dt>
<dd>Schedule a "well-bird" checkup. Prevention is the best medicine. Even though the bird might appear outwardly healthy, it may have a low-grade infection or something not so readily apparent. Your bird's health and your peace of mind will be worth it.</dd>

<dt>My bird'
s leg is being rubbed raw by the leg band. Can I take it off?</dt>
<dd>No. Don't attempt this, especially if the leg is broken or swollen. The vet will be able
to remove the band, and deal with whatever injury maybe lurking under the banded area.</dd>

<dt>How do I pull a broken blood feather?</dt>
<dd>This is probably the most common mishap. The remedy is simple--yank! It'
s most easily done
with two people. One to restrain the bird and the other to pull the feather. Use pliers, or a
hemostat. Tweezers won't work on primaries. Make certain that the wing bones are firmly supported
or you can break the wing. Clamp onto the feather and give a sharp tug in the direction of the
feather. The feather will come out. Next, apply gentle, direct pressure to the follicle where the
feather was to stop the bleeding. Dab some styptic powder on it, as it will help stop the bleeding
as well. Let the bird rest. Ask your vet or breeder to demonstrate exactly how to pull a blood
feather if you'
re apprehensive about doing it.</dd>
</dl>
</body>
</html>
  1. 透過 next() 可以取得 next sibling node

  2. 使用 end() 的優點在於 $("#faq") 的 DOM select 動作只會作一次,因此可以增加程式執行效能

  3. slideToggle() 就只是滑動的效果而已,不過官方文件中還可以另外指定 speed 跟 callback function

<html>                                                               
<head>
<script type="text/javascript" src="js/jquery-1.3.1.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
/* 一連串的動作,說明如下:
1、尋找 ID 為 FAQ 的 element
2、尋找該 element 下的 dd element,並將其隱藏
3、使用 end() 回到 faq element 狀態
4、繼續尋找 dt element,並進行 event regist
5、設定 click 後,會讓 dt element 的下一個 element 進行 slideToggle() 的動作 */

$("#faq").find('dd').hide().end().find('dt').click(function(){
$(this).next().slideToggle();
});
//使用 end() 的優點在於 $("#faq") 僅會發生一次 DOM select
});
-->
</script>

</head>
<body>
<h3>Bird FAQ - click the questions to show the answers</h3>
<dl id="faq">
<dt>What shouldn't I do to the bird?</dt>
<dd>Never use oils or lotions which contain oils on your bird. They gunk up the feathers,
and ruin their insulating properties. This means a chilled bird. Never wait out a cat bite--those
require immediate veterinary attention--a bird can die within two days because a cat'
s mouth is so
filthy and full of bacteria. Don't bother with over-the-counter medication. It really doesn't work,
and in some cases, may upset the delicate bacterial balance in the bird's body, or even worsen the
situation. Never try to treat a fracture at home.</dd>

<dt>My bird is healthy. I don'
t need to go to a vet, do I?</dt>
<dd>Schedule a "well-bird" checkup. Prevention is the best medicine. Even though the bird might appear outwardly healthy, it may have a low-grade infection or something not so readily apparent. Your bird's health and your peace of mind will be worth it.</dd>

<dt>My bird'
s leg is being rubbed raw by the leg band. Can I take it off?</dt>
<dd>No. Don't attempt this, especially if the leg is broken or swollen. The vet will be able
to remove the band, and deal with whatever injury maybe lurking under the banded area.</dd>

<dt>How do I pull a broken blood feather?</dt>
<dd>This is probably the most common mishap. The remedy is simple--yank! It'
s most easily done
with two people. One to restrain the bird and the other to pull the feather. Use pliers, or a
hemostat. Tweezers won't work on primaries. Make certain that the wing bones are firmly supported
or you can break the wing. Clamp onto the feather and give a sharp tug in the direction of the
feather. The feather will come out. Next, apply gentle, direct pressure to the follicle where the
feather was to stop the bleeding. Dab some styptic powder on it, as it will help stop the bleeding
as well. Let the bird rest. Ask your vet or breeder to demonstrate exactly how to pull a blood
feather if you'
re apprehensive about doing it.</dd>
</dl>
</body>
</html>
  1. 透過 parents(expr) 取得父節點,並指定 css 效果。



參考資料

2009年2月10日 星期二

[TCP/IP Illustrated] Broadcasting & Multicasting

前言

IP address 有三種,分別是:
  1. unicast
    此即為一般的 IP address,傳送者與接收者是一對一的關係。

  2. broadcast
    當要將訊息發布給同網段中所有的電腦時,就將訊息送給 broadcast IP address,這樣一來所有的電腦都會將訊息接收過去。使用此特性的協定有 ARP、RARP....等等。

  3. multicast
    此種也是類似 broadcast,但接收端僅為特定的群組,並非全部電腦都包含在內。

其中 broadcase 與 multicase 都必須仰賴 UDP 協定,原因是因為使用 TCP 必須先進行 3-way Handshake 以進行兩端點間的連線,並不適合用於此情境中。



Broadcasting

broadcast 是將資料傳送到網路中所有的電腦的一種方式,一共可以分為四種:(資料來源)
  1. 有限廣播 (Limited Broadcast)
    只有目前所在的區域網路 (LAN) 上作廣播,其IP的地址格式為255.255.255.255。

  2. 網路取向廣播 (Net-directed Broadcast)
    在某特定 (指定) 的網路作廣播,例如向某一類別為A (Class A) 的網路廣播,其IP地址格式為netid.255.255.255。

  3. 子網路取向廣播 (Subnet-directed Broadcast)
    針對某等級(class) 中的某子網路 (Subnetwork) 作廣播。以類別B的網路來看,若其子網路遮罩 (subnet mask) 為255.255.255.0,而IP地址為140.116.46.255,則是表示將資料廣播到類別B的網路140.116.中的子網路46上,而最後一個255則是代表廣播的意思。

  4. 全子網路取向廣播 (All-subnets-directed Broadcast)
    針對某一等級中所有的子網路皆作廣播。此種廣播方式必須與路由器 (router) 的子網路遮罩配合。例如有一類別B的子網路遮罩為255.255.255.0,則若有一IP地址為140.116.255.255時,則表示將資料廣播到類別B的網路140.116中的所有子網路上。在實際的網路架構中,歸屬於某一類別網路的所有子網路,可能位在不同路由器 (router) 的不同輸出入埠上(port),因此藉由子網路遮罩,路由器可以將資料傳送到所屬的所有子網路上進行廣播



Multicasting

IP Multicasting 是將資料傳送給某一特定群體 (Group) 的所有成員 (members),而屬於同一群體的各個成員可能是散佈在各個不同的網路上。

接著用一張圖來說明 multicasting 的 IP address 格式:

從上圖可以知道,multicasting IP address 屬於 class D,其範圍介於「224.0.0.0 ~ 239.255.255.255」之間

RFC 1112 定義了 IP multicast 的標準,它的特性之一是可送出一份 IP datagram 到 LAN 或 WAN 上,只要對這份 datagram 有興趣的使用者加入此「group address」(如何加入呢? 只要設定要擷取特定 multicasting IP address 的資料,就表示加入此 group 了;若是有其他認證機制則另當別論),就可收到這份 datagram。Multicasting 是一種 push 的技術,server 送出資料而 client 不用發出 request。

而 router 也要判斷所連結的網段中是否有加入特定 multicasting group 的電腦,來決定是否繼續傳送 multicasting 的訊息,這裡面運作的機制,稱為 IGMP(Internet Group Management Protocol)。

在目前還是有許多地方會應用到,最常用的大概就屬於視訊會議這一類的應用了。

然而,IANA 預先定義了幾個 well-known 的 multicasting IP address(就像 well-known port number 一樣),在使用上就必須要避開這些 IP,例如:
  • 224.0.0.1 (子網路中所有的系統)

  • 224.0.0.2 (子網路中所有的 router)

  • 224.0.1.1 (NTP;Netowork Time Protocol)

  • 224.0.0.9 (RIP-2)

當然不只以上這些,而若要查詢最新的清單,可以查詢 RFC 文件。


參考資料

[Oracle] 網路學習資源

ODP.NET (Oracle Data Provider for .NET)



TableSpace





Import & Export





Performance Tunning





SQL 語法應用





未分類










2009年2月9日 星期一

[TCP/IP Illustrated] UDP (User Datagram Protocol)

前言

不同於 TCP(stream-oriented,每個 IP datagram 中的資料都有其關聯) 協定,UDP 屬於不可靠的協定,因此並無法保證封包一定可以到達目的地(這表示即使封包沒到達目的地,發送端也不會收到任何通知)。

以下為 UDP datagram 是如何封裝在 IP datagram 中:

由於 UDP 協定必須依靠 IP 協定將資料傳送出去,因此在 UDP datagram 外面還要另外加上 IP datagram。



UDP Header

雖然 UDP 是種不可靠的協定,但相對於 TCP 單純的多,以下是 UDP Header 的內容:

其中比較需要說明的為 UDP length 的部份,而此 length 指的是整個 UDP header 的長度,最少為 8,而從圖中就可以看出,當 length = 8,表示 data 的部份為 0 byte。

而 checksum 的欄位,可以作為驗證資料正確與否來用,但接收端驗證後,若即使有資料錯誤的情況發生,也只會直接丟棄封包而不會通知傳送端,因此有些網路設備並沒有實作或是開啟驗證 checksum 的功能。



IP Fragmentation

要瞭解 IP 是如何將封包拆開再結合之前,必須先看看 IP header 的內容:
當資料量過大時,IP 會自動將資料拆開,分成多個 datagram 進行傳送。

其中將資料拆開成多個 datagram 的工作在傳送段電腦完成;而將多個 datagram 組合成原本的資料,則是接收段電腦的工作。

而在 IP header 中用來控制資料拆開與組合的資訊,即為 identification、flag、fragmentation offset 三個欄位。

此外,若是封包在傳遞的過程中遺失了應該要怎辦? 答案就是:「全部重送一次」。

但 IP 沒有可以判斷傳送是否發生錯誤的機制,因此必須仰賴上一層的 protocol(例如:TCP) 來處理,而 UDP 沒有此種驗證機制,因此若是有需求,就必須在 UDP application 中有提供才行。

那到底將資料拆成多個封包後會變得如何呢? 以下有圖可以說明:



UDP datagram 可以到多大?

理論上,每一個 UDP datagram 最大可以到 65535 bytes(20 bytes IP header + 8 bytes UDP header + 63307 bytes data),但實際上,由於 socket API 或是 OS kernel 的限制,有時候並無法使用這麼大的 UDP datagram 進行傳送。

而實際應用上,其實也沒用到如此大的 UDP datagram,舉例來說:

NFS:使用大小為 8192 bytes 的 UDP datagram

DNS、TFTP、BOOTP、SNMP:都使用小於 512 bytes 的 UDP datagram



與 UDP 協定相關的錯誤

ICMP Unreachable Error (Fragmentation Required)

當封包大小超過 MTU 時,就必須將封包拆開了,但此時如果 IP header 中的 DF(don't fragment) flag 若是被啟用,就會發生 ICMP Unreachable Error。


ICMP Source Quench Error

此種錯誤發生在接收端的 buffer 又已經滿了,且來不及處理快速傳入的 UDP datagram 時。



基本上,不一定每個接收端發生此種錯誤時,都會處理 ICMP Source Quench Error 訊息給傳送端,這必須要端看接收端的實作情況。(有些會因為協定是 UDP 而忽略,TCP 則會處理)