2008年11月10日 星期一

如何產生圖片縮圖

Bitmap bm= (Bitmap)Bitmap.FromFile(FileName);
int shtWidth, shtHeight;
if (bm.Width>bm.Height)
{
shtWidth = 250;//max width
shtHeight=bm.Height*shtWidth/bm.Width;
}
else
{
shtHeight = 250;//max height
shtWidth = bm.Width*shtHeight/bm.Height;
}
Bitmap resized = new Bitmap(shtWidth, shtHeight);
Graphics g = Graphics.FromImage(resized);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bm, new Rectangle(0, 0, resized.Width, resized.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel);
g.Dispose();
bm.Dispose();
resized.Save(SCANPREVIEWFILE, ImageFormat.Jpeg);

2008年10月17日 星期五

HTML中禁止自動完成和禁用輸入法

IE提供了一個自動完成功能可以記憶我們的輸入內容(如登錄帳號等),方便下一次快速地錄入類似資料.這確實是一個非常友好的功能, 在操作時只需用鼠標雙擊文本框或輸入前幾個字符, 系統會自動列出以前的錄入歷史供你選擇, 大大提高錄入速度及準確性. 相信大多數IE的用戶都啟用了這項功能.
然而做為網站開發者來說, 並不希望什麼資料都記憶在用戶的電腦上(如銀行帳號和其它重要的帳號及密碼), 但是我們又不能要求用戶禁用自動完成的功能. 幸好IE5.0後為INPUT type=password, INPUT type=text, FORM 等控件加入了一個AUTOCOMPLETE的屬性,要禁止控件自動完成功能,只需把AUTOCOMPLETE設為off即可,如:
整個表單禁止自動完成
FORM method=post action="submit.asp" AUTOCOMPLETE="OFF"
禁止文本框自動完成
input type="text" name="creditcard" maxlength="16" AUTOCOMPLETE="OFF"
在腳本中禁止自動完成
function init()
{
element.setAttribute("AutoComplete", "off");
}

另外,如果要禁止文本框使用輸入法,可以把在它的樣式中添加ime-mode : disabled 即可,但是這樣並不能禁止輸入漢字,因為用戶還是可以通過複製粘貼輸入漢字的.如:
input type=text style="ime-mode: disabled ; "
※chrome 不支援此用法,改為 type="tel" 可以達到相同效果

2008年10月7日 星期二

遠端桌面連線失敗!?

防火牆關了,IP也正常可以ping 到
但用遠端桌面連線時就是 一閃就沒了
而且是大家都連不過去那位仁兄的電腦 但他卻可以連大家
也沒有什麼錯誤訊息
後來GOOGLE一下 無意中發現有人是更換顯示卡驅動程式才解決此問題
於是在那位仁兄的電腦上更新最新的驅動程式就搞定了
囧囧囧

PS. 顯示卡為 nvidia 9800gt, old driver version:175.16, new driver version:178.13

2008年9月3日 星期三

GRIDVIEW正確處理多行文字方框換行之查詢結果

//with template
((Label)e.Row.FindControl("LabelAfterCourseStatus")).Text = ((Label)e.Row.FindControl("LabelAfterCourseStatus")).Text.Replace("\r\n", "<br/>");

//without template
e.Row.Cells[1].Text=e.Row.Cells[1].Text.Replace("\\n", "<br/>");

PS. 請將<>改成小寫

2008年8月15日 星期五

SQL SERVER 2005 如何開啟異動遠端連結伺服器的資料

To allow the network transaction, you must enable MSDTC. To do this, follow these steps:
a. Click Start, and then click Run.
b. In the Run dialog box, type dcomcnfg.exe, and then click OK.
c. In the Component Services window, expand Component Services, expand Computers, and then expand My Computer.
d. Right-click My Computer, and then click Properties.
e. In the My Computer Properties dialog box, click Security Configuration on the MSDTC tab.
f. In the Security Configuration dialog box, click to select the Network DTC Access check box.
g. To allow the distributed transaction to run on this computer from a remote computer, click to select the Allow Inbound check box.
h. To allow the distributed transaction to run on a remote computer from this computer, click to select the Allow Outbound check box.
i. Under the Transaction Manager Communication group, click to select the No Authentication Required option.
j. In the Security Configuration dialog box, click OK.
k. In the My Computer Properties dialog box, click OK.

SQL SERVER trigger 中的 SQL 語法範例

update

update a set c1=i.c1 from inserted i,table1 a where i.id=a.id;

insert

insert into table1 (c1) select c1 from inserted;


inserted 代表新增或更新的資料列表格

2008年8月4日 星期一

善用 StringBuilder 類別

通常許多人處理字串串連時,都會用 + 這個運算子來處理
但是這雖然也是可以正常處理字串相加,不過對系統效能上卻造成極大的負擔
這是因為使用 string 這個類別時會配置一段記憶體來儲存字串
而當以 + 運算子處理字串相加時或字串資料變更時
系統必須配置一段新的記憶體來儲存新字串的動作 (如兩字串相加後的新字串)
再反配置舊的記憶體
每次配置記憶體和反配置都會造成 CLR 的工作
當然這些動作都是在幕後處理完成,每次處理的時間非常的短
但是如果大量處理這些字串運算或大塊記憶體時,累加起來的時間就不只是如此了
尤其是在 Web 伺服器上執行的 ASP.Net 應用程式來說更是會造成嚴重的影響

而 StringBuilder 物件則不同,它會保留自己的字串緩衝區
所有的動作都在同一塊記憶體工作,減少記憶體配置的動作
除非緩衝區大小不夠處理時,才會作記憶體重新配置
自然能有效提升效能

原文出處

2008年7月9日 星期三

關閉 vista superfetch

使用 Windows Vista 時,會發現一開完機,系統就會拼命的使用記憶體,一直到用光為止,這是因為 Superfetch 功能,它會預先載入 (快取) 你之前常用的程式和檔案到記憶體中,以便於加速程式啓動的速度,你可以在以下 Registry 中,找到一個名為 EnablePrefetcher 的機碼值:
代碼:
HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management\PrefetchParameters

EnablePrefetcher 這個值的設定:
代碼:
0=停用
1=Superfetch 應用程式
2=Superfetch 開機程序
3=Superfetch 兩者


你也可以將 Superfetch 這個服務設定成停用,完全不使用 Superfetch 功能。

另外,Vista 的另一項功能 ReadyBoost,可以進一步改善 Superfetch 的效能,它可以讓整個快取流程加速,改由從隨身碟將常用的程式和檔案載入記憶體,而不是從硬碟。

如果要停用 ReadyBoost 功能,可以直接停用 ReadyBoost 這個服務。

2008年7月7日 星期一

防止表單被重複 Submit 送出

方法1.
<body onbeforeunload="oIn=document.getElementsByTagName('INPUT');for(i=0; i<oIn.length; i++){oIn(i).disabled=true};">
PS. 此方法會跟 listview 的 NumericPagerField DataPager 及 BWSLib.DefaultGrid 相衝

方法2.
於網頁上放置ScriptManager,UpdatePanel
將送出按鈕放置於UpdatePanel中
於網頁最後加入下面程式

<script language="javascript">
// Get a reference to the PageRequestManager.
var prm = Sys.WebForms.PageRequestManager.getInstance();

// Using that prm reference, hook _initializeRequest
// and _endRequest, to run our code at the begin and end
// of any async postbacks that occur.
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

// Executed anytime an async postback occurs.
function InitializeRequest(sender, args)
{
// Get a reference to the element that raised the postback,
// and disables it.
$get(args._postBackElement.id).disabled = true;
}

// Executed when the async postback completes.
function EndRequest(sender, args)
{
// Get a reference to the element that raised the postback
// which is completing, and enable it.
$get(sender._postBackSettings.sourceElement.id).disabled = false;
}
</script>

2008年5月7日 星期三

SQL Server how to update table from another table's value

update r set associater=c.coursegroup from courseassistantlist c ,recommendassociate r where r.createcourseid=c.createcourseid

2008年4月29日 星期二

ASP.NET 如何讓 GridView 強制使用指定之欄寬

於 rowdatabound 事件中加入

e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;");

2008年3月31日 星期一

淑芬傳奇後傳

「廖淑芬.60分!你呀,不是名字叫起來像 60分就可以考60分啊?!」
老師不悅地說。 .....
「伍淑芬.50分!你呀!比廖淑芬還不如!」老師依舊忿忿然地說著接著,老師以更生氣的語調發
著下一張考卷:
「柯淑芬(台語).10分!喔,你也一樣!名字叫起來像10分你就給我考10分?!」
.........

這時...「啊!我完了 ....」...柯淑芬的妹妹心中開始暗叫不妙...............
!
!
!
!
!
!
!
!
!
!
老師嘆口氣,很無奈的發著第四張:「不是我在說你們這對姊妹實在是太不像話了...」

.........

「柯玲芬(台語)...0分!」

老師搖搖頭道:

「你們兩姐妹應該向你們哥哥好好看齊,他總是考滿分,你們倆不要老是考那樣的成績。」

!

!

!

柯淑芬和柯玲芬兩姐妹心裡暗罵,「都嘛是爸媽把哥哥的名字取得太好了,叫做...




!!!!!」

2008年3月14日 星期五

SQL Server 的自動編號欄位運用

在資料庫中, 常用的一個流水編號通常會使用 identity 欄位來進行設置, 這種編號的好處是一定不會重覆, 而且一定是唯一的, 這對table中的唯一值特性很重要, 通常用來做客戶編號, 訂單編號等功能, 以下介紹關於此種欄位常用方式及相關技術.

後面的範例表皆以此表為建立:
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))

1. 在程式中取得identity值:
因為 identity 特性, 所以在 insert into 該 table 時, 不能指定該 identity 欄位值, 僅能指定其他欄位值, 而 identity 由資料庫維護, 所以一般要在 insert 後取得該 identity 欄位值, 則通常使用下面方式:
insert into products (product) values ('saw')
select @@identity
利用全域變數 @@identity 來取得最後影響的 insert 後產生的 identity 值, 如此一來便能方便地使用 identity 欄位.

2. 若要啟用識別插入(identity insert)時, 也就是如空缺號要指定 identity 欄位值時, 或者是處理資料表整理或備出時, 會用到的方式:
set identity_insert products on
insert into products (id, product) value (3, 'screwdriver')
要注意的地方是可以 insert 空缺號, 也可以加至最後, 但系統會自動更新 identity 至最大值, 要注意一旦啟用 identity_insert 時, 就一定要給定 identity 值, 另外並不能 update 該 identity 欄位值, 也就是說 identity_insert 該 identity 欄位僅 for insert, 不能 update.

3. 查詢目前 identity 值:
有時我們需要查詢目前 table 中該 identity 欄位最大值是多少時, 可以利用 dbcc 指令, 如下:
dbcc checkident('product', NORESEED)
可以獲得目前最大值的結果.

4. 重設目前最大 identity 值:
一樣利用 dbcc 指令, 如下:
dbcc checkident('product', RESEED, 100)
如此一來, 便能將目前的最大 identity 值指向100, 當然若故意設比目前最大值小時, 系統仍會接受, 但若 identity 遇上重覆資料時(如將 identity 設為 primary key時), 將會發生重大問題, 該 table 變成無法 insert 資料, 因為會發生 primary key violation, 解決方法當然就是將目前的 identity 修復, 直接使用
dbcc checkident('product', RESEED) 或
dbcc checkident('product')
(兩者等義)即可順利修復.

5. identity 欄位遇上 rollback 時:
當 identity 欄位碰到 rollback 時, 會發生跳號現象, 也就是說在 transaction 中, insert 了一筆資料, 但又 rollback 時, 該 identity 號會消失, 如下測試:
begin tran
insert into products (product) values ('test rollback')
rollback tran
dbcc checkident('product', NORESEED)
這個觀念很重要, 因為要維持 identity 特性, 但又發生 rollback, 所以系統就直接跳號處理囉, 避免發生重覆編號的問題.

2008年3月10日 星期一

SQL SERVER 2005 如何於查詢結果加上序號欄位

select rank() OVER (ORDER BY a.au_lname, a.au_fname) as rank, a.au_lname, a.au_fname from authors a order by rank

2008年2月27日 星期三

把網頁當作物件來操作

重點在兩個語法:Server.Transfer和Context.Handler

下面是簡單範例

網頁A按下按鈕後導向到網頁B
private void Button1_Click(object sender, System.EventArgs e)
{
  Server.Transfer("anotherwebform.aspx");
}

網頁B把網頁A當作物件來使用
private void Page_Load(object sender, System.EventArgs e)
{
  //create instance of source web form
  WebForm1 wf1;
  //get reference to current handler instance
  wf1=(WebForm1)Context.Handler;
  Label1.Text=wf1.Name;
  Label2.Text=wf1.EMail;
}

不過盡量別濫用
畢竟網頁應該是屬於使用者介面層物件
還是要跟商業邏輯層的物件區隔
不要包成一大包會比較清楚明白

2008年2月14日 星期四

為何gridview無法依照我指定的itemstyle對齊

試了半天才發現
原來是因為所有的欄位都指定寬度造成的
請不要指定最後一欄的寬度
問題就會消失囉
希望20年(?)後若發生同樣問題 我還記得回來查詢原因 不要又試半天

如何於updatepanel1中按下按鈕後更新updatepanel2

updatepanel1內含button1
updatepanel2內含sqldatasource1,gridview1
於Button1_Click()中會異動資料庫並造成sqldatasource1的結果有所改變,必須即時更新gridview1內容
則於更新資料後要執行SqlDataSource1.DataBind(),GridView1.DataBind()
且updatepanel2加入非同步回傳觸發針對button1之click事件
如此便可即時更新gridview1

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...