public static bool IsDate(Object obj)
{
string strDate = obj.ToString();
try
{
DateTime dt = DateTime.Parse(strDate);
if((dt.Month!=System.DateTime.Now.Month) || (dt.Day<1&&dt.Day>31) || dt.Year!=System.DateTime.Now.Year)
return false;
else
return true;
}
catch
{
return false;
}
}
2014年6月3日 星期二
2014年5月24日 星期六
相除取整數及餘數C# -VB div mod
int a = 5;
int b = 3;
int div = a / b; //div is 1
int mod = a % b; //mod is 2
C#運算子
C#運算子 |
指定資料運算方法的指令,可分為指定、複合指定、算術、關係、邏輯、字串、位元等... |
指定運算子(Assignment) |
就是等於(=)這個符號,用來把資料指定給變數 |
字串運算子(String) |
連接字串用的運算字,就是加號(+) |
複合指定運算子 | |||
運算子符號 | 功能 | 範例 | 結果 |
+= | 相加後指定 | a=1;a+=2; | 3 |
-= | 相減後指定 | a=1;a-=2; | -1 |
*= | 相乘後指定 | a=1;a*=2; | 2 |
/= | 相除後指定 | a=4;a/=2; | 2 |
%= | 取餘數後指定 | a=5;a%=2; | 1 |
&= | AND運算後指定 | a=3;a&=5; | 1 |
|= | OR運算後指定 | a=3;a|=5; | 7 |
算術運算子(Arithmetic) | |||
數學運算 | |||
運算子符號 | 功能 | 範例 | 結果 |
+ | 加法 | 1+2 | 3 |
- | 一元運算子為取負數,二元運算子為減法 | -1或是1-2 | -1 |
* | 乘法 | 1*2 | 2 |
/ | 除法 | 4/2 | 2 |
% | 取餘數 | 5%2 | 1 |
++ | 前置為為先運算後再遞增,後置為先遞增後再運算 | x=1;x++ | x的值為2 |
-- | 前置為為先運算後再遞減,後置為先遞減後再運算 | x=1;x-- | x的值為0 |
關系運算子(Relational) | |||
比大小,結果傳回布林值 | |||
運算子符號 | 功能 | 範例 | 結果 |
== | 等於 | 1 == 2 | false |
!= | 不等於 | 1 != 2 | true |
< | 小於 | 1 < 2 | true |
> | 大於 | 1 > 2 | false |
<= | 小於等於 | 1 <= 2 | true |
>= | 大於等於 | 1 >= 2 | false |
邏輯運算子(Logical) | |||
布林運算 | |||
運算子符號 | 功能 | 範例 | 結果 |
& | AND | false & true | false |
&& | 快速的AND,前面false就不運算後面,傳回false | false && true | false |
| | OR | true | false | true |
|| | 快速的OR,前面true就不運算後面,傳回true | true || false | true |
! | NOT | ! true | false |
^ | 互斥XOR | true ^ false | true |
位元運算子 | |||
二進位運算 | |||
運算子符號 | 功能 | 範例 | 結果 |
& | AND運算 | 3 & 5 | 1 |
| | OR運算 | 3 | 5 | 7 |
^ | 互斥XOR | 3 ^ 5 | 6 |
~ | 取補數 | ~ 1 | -2 |
>> | 右移,就是除以2取商 | 3 >> | 1 |
<< | 左移,就是乘以2 | 3 << | 6 |
運算子優先順序 | |
運算的優先順序,由上到下,不確定順序的話,用小括號包起來就對了 | |
分類 | 運算子 |
區塊 | 用()包起來的先運算 |
一元運算子 | 取負數、++、-- |
乘除 | *、/ |
加減 | +、- |
位移 | <<、>> |
關系 | <、>、<=、>= |
關系 | ==、!= |
AND | & |
XOR | ^ |
OR | | |
快速AND | && |
快速OR | || |
指定和複合指定 | =、+=、-+、*=、/=、<<=、>>=...... |
2014年5月21日 星期三
VB的IsDBNull C#的 Convert.IsDBNull() 函數用法
Convert.IsDBNull()返回有關指定物件是否為 DBNull 類型的指示,即是用來判斷物件是否為DBNULL的。其返回值是True或Flase。
VB
IsDBNull(objDataReader_name["my_all"]) == true
C#
Convert.IsDBNull(objDataReader_name["my_all"]) == true
VB
IsDBNull(objDataReader_name["my_all"]) == true
C#
Convert.IsDBNull(objDataReader_name["my_all"]) == true
2014年3月30日 星期日
GridView72絕技-文字變顏色
前台展示結果:
後台程式碼:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim oRow As Data.DataRowView
Dim oFontColor As System.Drawing.Color = Drawing.Color.Empty
If e.Row.RowType = DataControlRowType.DataRow Then
oRow = CType(e.Row.DataItem, Data.DataRowView)
'文字變顏色條件
If oRow("欄位名稱") < 1 Then
oFontColor = Drawing.Color.Black
Else
oFontColor = Drawing.Color.Red
End If
e.Row.ForeColor = oFontColor
End If
End Sub
2014年3月7日 星期五
gridview72絕技之一-螢光棒效果DataControlRowType
前台-必須加上OnRowDataBound="GridView1_RowDataBound
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
Font-Bold="True" Font-Size="Medium" PageSize="12" Width="1200px"
DataKeyNames="uid" OnRowDataBound="GridView1_RowDataBound">
=======================================================================
後台
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim i As Integer
For i = 0 To GridView1.Rows.Count - 1
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#CEFF63'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c")
End If
Next
End Sub
2014年3月2日 星期日
資料庫及檔案CONNECTION語法
1.Access數據庫的DSN-less連接方法:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("數據庫所在路徑")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("數據庫所在路徑")
2.Access OLE DB連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("數據庫所在路徑")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("數據庫所在路徑")
3.SQL server連接方法:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=數據庫名;"
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=數據庫名;"
4.SQL server OLE DB連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=數據庫名"
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=數據庫名"
5.Oracle 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
7.dBase 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
8.mySQL 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"
adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
10.MS text 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
11.MS text OLE DB 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
2014年2月20日 星期四
Page_Unload關閉資料釋放記憶體
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
objcmd.Dispose()
objcmd = Nothing
objcon.Close()
objcon.Dispose()
objcon = Nothing
GC.Collect(0, System.GCCollectionMode.Forced)
End Sub
2013年10月5日 星期六
ASP.NET-JavaScript alert用法
由於本身也在寫一些ASP.NET,以後也把一些遇到的問題和解決方式寫出來,順便幫自己做筆記。
在程式設計時可以用Msgbox來顯示警告視窗,但移到正式環境就會出錯。
所以在Client顯示警告視窗可以用
Response.Write(" ")
或在頁面上加入一個Literal控制項再加入
Me.Literal1.Text = " "
Response.Write(" ")
或在頁面上加入一個Literal控制項再加入
Me.Literal1.Text = " "
另外導入頁面可以用
Response.Write(" ")
或
Response.Redirect("index.aspx")
Response.Write(" ")
或
Response.Redirect("index.aspx")
但是將兩個功能放在一起,就會直接導到頁面,不會出現警告訊息。
這時可以在最後加入Response.End()即可解決(但不能使用Response.Redirect)另外用window.open會被瀏覽器擋住,所以可以使用location.href或window.location來取代,又由於location.href有些瀏覽器已經不支援,故建議使用window.location
範例如下
Response.Write(" ")
Response.End()
Response.Write(" ")
Response.End()
以上皆為後置程式碼(.vb或.cs)內之程式。
滑鼠移動改變按鈕圖片,onmouseover,onmouseout
'使用HTML語法+java script可以得到效果
'圖片檔案格式建議jpg或gif
<a href="/user_power.aspx" target="_parent">
onmouseover="this.src='/test/test_over.gif'" onmouseout="this.src='/test/test.jpg'"border="0" title="ASP.NET提示訊息" />
</a>
textbox配合PopupControlExtender-選擇日期
'前台
PopupControlID="pnlCalendar" TargetControlID="tbReturn" Position="Bottom" >
'後台
Protected Sub c1_SelectionChanged(sender As Object, e As System.EventArgs) Handles c1.SelectionChanged
Me.PopupControlExtender1.Commit(Me.c1.SelectedDate.Date)
End Sub
AnimationExtender 淡入淡出onload
<asp:AnimationExtender ID="Menu1_AnimationExtender" runat="server" TargetControlID="Panel1">
<Animations>
<OnLoad>
<Sequence>
<FadeIn AnimationTarget="Panel1" Duration="5" Fps="20" maximumOpacity="0.8" minimumOpacity="0.1">
</FadeIn>
</Sequence>
</OnLoad>
</Animations>
</asp:AnimationExtender>
<Animations>
<OnLoad>
<Sequence>
<FadeIn AnimationTarget="Panel1" Duration="5" Fps="20" maximumOpacity="0.8" minimumOpacity="0.1">
</FadeIn>
</Sequence>
</OnLoad>
</Animations>
</asp:AnimationExtender>
2013年10月4日 星期五
計算兩日期時間差異天數TimeSpan Subtract
Dim sd As Date = TextBox_sd1.Text
Dim ed As Date = TextBox_ed1.Text
'計算兩日期時間差
Dim vd As TimeSpan = ed.Subtract(sd)
'差異天數
Dim my_vd As Integer = vd.TotalDays
2013年10月3日 星期四
2013年10月2日 星期三
日期時間差範例
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bft As Date = TextBox1.Text
Dim att As Date = TextBox2.Text
Dim min As TimeSpan = att.Subtract(bft)
Dim my_min As Integer = min.TotalMinutes
Response.Write("時間差(分):" & my_min)
End Sub
2013年9月25日 星期三
SQLserver 時間預設值
1.日期時間
(getdate())
(getdate())
2.今天日期
(CONVERT([smalldatetime],(((CONVERT([char](4),datepart(year,getdate()),(0))+'-')+CONVERT([char](2),datepart(month,getdate()),(0)))+'-')+CONVERT([char](2),datepart(day,getdate()),(0)),(0)))

3.SQL語法
DateTime a = DateTime.Now;
string sql="Insert into test(這裡要指定欄位名稱) values('" + a.ToSTring("yyyy/MM/dd HH:mm:ss") + "')";
string sql="Insert into test(這裡要指定欄位名稱) values('" + a.ToSTring("yyyy/MM/dd HH:mm:ss") + "')";
或者,直接寫成
string sql="Insert into test(這裡要指定欄位名稱) values(getdate())";
即可
string sql="Insert into test(這裡要指定欄位名稱) values(getdate())";
即可
ps.如果直接下DateTime.now會產生字串問題
介紹DLL不錯的文章
為什麼要使用 DLL (Dynamic Linking Library) - 動態連結檔 ?
微軟當初為Windows設計動態連結檔主要是擷取它的兩項優點:一是動態連結、一是資源共享。資源共享的例子相當顯而易見,例如之前曾經提過Windows有三個核心的動態連結檔:Kernel主要是負責系統和應用程式的記憶體、行程和執行緒等等的管理工作;User主要負責使用者介面和訊息的傳遞;GDI則負責系統的任何圖形繪製、顯示等工作。而這些動態連結檔所提供的任何函數都可以在必要的時候,讓每一個Windows環境底下的執行檔使用。因為DLL具備節省記憶體的特性,因此自從Windows 3.1版以來,它就逐漸成為Windows程式設計的主流
微軟當初為Windows設計動態連結檔主要是擷取它的兩項優點:一是動態連結、一是資源共享。資源共享的例子相當顯而易見,例如之前曾經提過Windows有三個核心的動態連結檔:Kernel主要是負責系統和應用程式的記憶體、行程和執行緒等等的管理工作;User主要負責使用者介面和訊息的傳遞;GDI則負責系統的任何圖形繪製、顯示等工作。而這些動態連結檔所提供的任何函數都可以在必要的時候,讓每一個Windows環境底下的執行檔使用。因為DLL具備節省記憶體的特性,因此自從Windows 3.1版以來,它就逐漸成為Windows程式設計的主流
將HTML標籤轉成文字-HtmlEncode
'在前台<%@ Page %>'插入ValidateRequest="false"
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" ValidateRequest="false"%>
' Visual BasicPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userinput As String = TextBox1.Text
userinput = Server.HtmlEncode(userinput)' Accepts , , , , case-insensitive
userinput = userinput.Replace("<b>", "")
userinput = userinput.Replace("</b>", "")
userinput = userinput.Replace("<B>", "")
userinput = userinput.Replace("</B>", "")
userinput = userinput.Replace("<u>", "")
userinput = userinput.Replace("</u>", "")
userinput = userinput.Replace("<U>", "")
userinput = userinput.Replace("</U>", "")
Label1.Text = userinput
End Sub
Dim userinput As String = TextBox1.Text
userinput = Server.HtmlEncode(userinput)' Accepts , , , , case-insensitive
userinput = userinput.Replace("<b>", "")
userinput = userinput.Replace("</b>", "")
userinput = userinput.Replace("<B>", "")
userinput = userinput.Replace("</B>", "")
userinput = userinput.Replace("<u>", "")
userinput = userinput.Replace("</u>", "")
userinput = userinput.Replace("<U>", "")
userinput = userinput.Replace("</U>", "")
Label1.Text = userinput
End Sub
訂閱:
文章 (Atom)