顯示具有 Code 標籤的文章。 顯示所有文章
顯示具有 Code 標籤的文章。 顯示所有文章

2009年2月4日 星期三

亂數得獎名單一次搞定!!

在接案子的過程中,總是會遇到一種程式,那就是投票。

隨投票而來的附加程式就是,抽獎!!

而抽獎最公平的方式就是,亂數!!(投票 = 抽獎 = 亂數 = 程式設計師該死,冏)

不過,身為一個程式設計員,偷懶是必備的人格特質…呃…我是說精進,所以下面的語法將增加你喝咖啡打混的時間…不…我是說提升工作效率,增加公司生產力!!

SELECT TOP 10 * FROM  ORDER BY NEWID()

利用NEWID()這個方式,取得每一筆Record的GUID(唯一值),這個唯一值是由SQL自已產生的,同時!!它剛好每執行一次就隨機產生一次,所以…嘿嘿嘿…

GUID的註釋如下:
Guid can be use for saving unique identifier in an SQL database. Its useful in creating a unique key for a particular session.rel

2008年10月17日 星期五

用文字指定DropDownList的SelectIndex

Visual Studio 2005 是很好用的一個工具,不過,好用的功能也得會用才行 = ="

以往在設定DropDownList(html tag:select)選項的時候…

都是認命的寫一個迴圈每一個item(html tag:option)去找符合的字串,再指定對應的index
(我只會用這個,別吵!!)

現在用一行語法就搞定啦!

Dim oDDL As DropDownList = 要指定index的DropDownList
oDDL.SelectedIndex = oDDL.Items.IndexOf(oDDL.Items.FindByValue(符合的字串));

打完,收工!

參考網址

2008年10月6日 星期一

資料表欄位重新命名

因為工作需求,開始碰Oracle跟Toad(我討厭青蛙)

結果建好的Oracle資料表居然沒辦法直接Rename欄位啊~~~ (Oracle你讓我太快懷念微軟了吧)

還好G大(Google)跟我說,可以用SQL語法去改… 嗯…G大,你真好T ^ T

語法如下,記得資料表中不能有資料喔!!

ALTER TABLE "TABLE_NAME"
RENAME COLUMN "OLD_COLUMN_NAME" TO "NEW_COLUNM_NAME"

2008年6月17日 星期二

不重複的亂數(ASP)

'ProduceRan(開始範圍, 結束範圍, 取多少個),註結束範圍-開始範圍需大於所取數目
call ProduceRan(1,101,100)

'產生n~m不重複亂數列
Sub ProduceRan(staNum,endNum,Validx)
if (endNum - staNum) < Validx then
 response.write "欲取數目大於設定範圍,無法計算!!"
 exit sub
end if

x = Validx
Dim theRanArray()
ReDim theRanArray(x)

'設定陣列初始值
for ValIdx = 0 to UBound(theRanArray)
 theRanArray(ValIdx) = 0
next

'與已知亂數比對,若重複則再取一次
for theRanIdx = 0 to UBound(theRanArray)
 theRan = ranValue(staNum,endNum)

 doubled = 0
 for checkIdx = 0 to UBound(theRanArray)
  if theRan = theRanArray(checkIdx) then doubled = 1 end if
 next

 if doubled = 0 then
  theRanArray(theRanIdx) = theRan 
 else
  theRanIdx = theRanIdx - 1 
 end if
next

'印出最後產生完成的亂數陣列
for prtIdx = 0 to UBound(theRanArray)
response.write theRanArray(prtIdx) & "
"
next

End Sub


'取得亂數函數
Function ranValue(beginNum,endNum)
 ' Initialize the random-number generator.
 Randomize()
 ' Generate random value between 1 and 6.
 ranValue = CInt(Int((endNum * Rnd()) + beginNum))
End Function


2008年5月15日 星期四

Column Style ( 4 x 4 )

<col>是一個相當方便的 Tag,想指定<table>中欄位的顏色,在<table>下面加上與欄位總數相同的<col> Tag,就可以分別指定各欄的樣式了。

Col1Col2Col3Col4
DataDataDataData
DataDataDataData
DataDataDataData
<table id="colStyle">

<col class="colStyle">
<col class="">
<col class="colStyle">
<col class="">

<tr>
<th>Col1</th><th>Col2</th><th>Col3</th><th>Col4</th>
</tr>
<tr>
<td>Data</td><td>Data</td><td>Data</td><td>Data</td>
</tr>
<tr>
<td>Data</td><td>Data</td><td>Data</td><td>Data</td>
</tr>
<tr>
<td>Data</td><td>Data</td><td>Data</td><td>Data</td>
</tr>
</table>

2008年5月12日 星期一

FindControlEx

Function FindControlEx(ByVal Parent As System.Web.UI.Control, ByVal ID As String) As System.Web.UI.Control
        Dim oCtrl As System.Web.UI.Control = Nothing
        Dim oChildCtrl As System.Web.UI.Control = Nothing

        '先使用 FindControl 去尋找指定的子控制項  
        oCtrl = Parent.FindControl(ID)

        '若尋找不到則往下層遞迴方式去尋找() 
        If oCtrl Is Nothing Then
            For Each oChildCtrl In Parent.Controls
                '以遞迴方式呼叫原函式 
                oCtrl = FindControlEx(oChildCtrl, ID)
                '若有尋找到指定控制項則離開迴圈 
                If oCtrl IsNot Nothing Then Exit For
            Next
        End If
        Return oCtrl
End Function
Source

ExtractRowValues

Function ExtractRowValues(ByVal Columns As DataControlFieldCollection, ByVal Row As GridViewRow) As OrderedDictionary
        Dim oFieldValues As OrderedDictionary
        Dim oColumn As DataControlField
        Dim oDictionary As OrderedDictionary
        Dim oEntry As DictionaryEntry
        Dim N1 As Integer

        oFieldValues = New OrderedDictionary(Columns.Count)
        oDictionary = New OrderedDictionary()

        For N1 = 0 To Columns.Count - 1
            oColumn = Columns.Item(N1)
            If oColumn.Visible Then
                oDictionary.Clear()
                oColumn.ExtractValuesFromCell(oDictionary, TryCast(Row.Cells.Item(N1), DataControlFieldCell), Row.RowState, True)
                For Each oEntry In oDictionary
                    oFieldValues.Item(oEntry.Key) = oEntry.Value
                Next
            End If
        Next

        Return oFieldValues
    End Function
Source