2009年12月4日 星期五

SQL Connection Dispose or Close

在asp裡開啟了資料庫連線,使用到最後,總要把連線關閉,只是connection.close()跟connection.dispose(),到底適合用在什麼時候,在google之後,找到相關的說明如下:

There seems to be a lot of confusion about how to clean up after using a SqlConnection object. Should you call Close(), Dispose(), Close() then Dispose(), or neither?

Here are some relevant facts we need to consider:


When an open SqlConnection goes out of scope, its underlying physical database connection will not be closed or returned to the connection pool by the garbage collector;
Dispose() always calls Close() implicitly;
Dispose() clears the ConnectionString, Close() does not;
In future versions of ADO.NET, the SqlConnection.Dispose method might free other unmanaged resources, in addition to the database connection.
What conclusions can we draw?


We must at least call Close() or Dispose(), otherwise the database connection won't be released;
There's no need to call both Close() and Dispose();
If we're going to open the connection again, we should Close() not Dispose();
When we're completely finished with the SqlConnection, we should call Dispose() to make sure that all unmanaged resources are released, both now and in the future.

The tempation of symmetry after calling Open() is to always call Close(), as was the case in classic ADO, but I've shown that in the case of SqlConnection we only need to call Dispose(). Better still, make sure Dispose() is always called implicitly by enclosing your SqlConnection objects in a using statement.


參考資料

2009年11月22日 星期日

LINQ在更新 VARBINARY欄位時的問題

之前在UPDATE VARBINARY時,總會出現一個EXCEPTION:

型別 'System.Collections.Generic.IList`1[System.Object]' 不支援比較運算子

檢查了對應到SQL SERVER裡的dbml中的欄位敘述
[Column(DbType = "varbinary(MAX)"]
public IList <object> arrayData;

多加了一個UpdateCheck的設定就能正常更新了。
[Column(DbType = "varbinary(MAX)", UpdateCheck = UpdateCheck.Never)]public IList<object> blockAry;

據說,加了這個UpdateCheck = UpdateCheck.Never之後,LINQ 在更新 Binary 格式的欄位時就不會在去判斷檔案的內容是否有異動,直接將更新的檔案內容直些寫入資料庫。

參考資料

2009年11月18日 星期三

MSSQL Data Type 2

bigint 整數 (完整數字) 資料從 -2^63 (-9223372036854775808) 至 2^63-1 (9223372036854775807)。

int 整數 (完整數字) 資料從 -2^31 (-2,147,483,648) 至 2^31 - 1 (2,147,483,647)

smallint 整數資料,從 2^15 (-32,768) 到 2^15 - 1 (32,767)。

tinyint 整數資料,從 0 到 255。

bit bit 整數資料,其值為 1 或 0。

decimal 與 numeric Decimal 固定位數及小數位數的數字 (Numeric) 資料是從 -10^38 +1 到 10^38 1。 Numeric 在功能上,等於 decimal。

money 與 smallmoney money 貨幣資料值從 -2^63 (-922,337,203,685,477.5808) 到 2^63 - 1 (+922,337,203,685,477.5807),精確度到千分之十貨幣單位。 smallmoney 貨幣資料值從 -214.748,3648 到 +214,748.3647,精確度到千分之十貨幣單位。

近似數字 float 浮點位數的數字資料,從 -1.79E + 308 到 1.79E + 308。

real 浮點位數的數字資料,從 -3.40E + 38 到 3.40E + 38。

datetime 與 smalldatetime datetime 日期與時間資料,從 1753 年 1 月 1 日到 9999 年 12 月 31 日,精確度為三百分之一秒,即 3.33 毫秒 (millisecond)。 smalldatetime 日期與時間資料,從 1900 年 1 月 1 日到 2079 年 6 月 6 日,精確度為一分鐘。

字元字串 char 固定長度的非 Unicode 字元資料,最大長度為 8,000 個字元。

varchar 可變長度的非 Unicode 資料,最大長度為 8,000 個字元。

text 可變長度的非 Unicode 資料,最大長度為 2^31 - 1 (2,147,483,647) 個字元。

Unicode 字元字串 nchar 固定長度的非 Unicode 資料,最大長度為 4,000 個字元。

nvarchar 可變長度的 Unicode 資料,最大長度為 4,000 個字元。

sysname 是一個系統支援的使用者自訂資料型別,為 nvarchar(128) 的同義資料表,用來參考資料庫物件名稱。

ntext 可變長度的 Unicode 資料,最大長度為 2^30 - 1 (1,073,741,823) 個字元。

二進位字串 binary 固定長度的二進位資料,最大長度為 8,000 個位元組。

varbinary 可變長度的二進位資料,最大長度為 8,000 個位元組。

image 可變長度的二進位資料,最大長度為 2^31 - 1 (2,147,483,647) 個位元組。


資料轉載於:http://topic.csdn.net/t/20041011/17/3445178.html

MSSQL Data Type

下圖轉載於 http://www.dotblogs.com.tw/limingstudio/archive/2009/11/12/11571.aspx