2011年11月28日 星期一

c# 驗證日期

以下程式碼為網路上找的
但因日期時間過久所以無法知道出處
記得先using阿!!
using System.Text.RegularExpressions;//導入命名空間(正規表達式)



/// <summary>
/// 判斷是否為日期
/// </summary>
/// <param name="pDate">傳入字串。</param>
public static bool IsDate(string pDate)
{
return Regex.IsMatch(pDate, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
}



/// <summary>
/// 判斷是否為時間
/// </summary>
/// <param name="pTime">傳入字串。</param>
public static bool IsTime(string pTime)
{
return Regex.IsMatch(pTime, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
}



/// <summary>
/// 判斷是否日期+時間
/// </summary>
/// <param name="pDateTime"></param>
public static bool IsDateTime(string pDateTime)
{
return Regex.IsMatch(pDateTime, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
}



/// <summary>
/// 回傳yyyy/MM/dd
/// </summary>
/// <param name="pDate">傳入字串。</param>
public static string ToyyyyMMdd(string pDate)
{
string mReturn = string.Empty;

if (pDate != "")
{
mReturn = DateTime.Parse(pDate).ToString("yyyy/MM/dd");
}
return mReturn;
}

c# 使用System.Net.Mail.SmtpClient寄信(無需帳密)

用樣寄是很好啦不需要登入帳密
但垃圾信太多小心被封了哈!!


1.web.config要加下面這段


<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" host="msa.hinet.net" port="25"/>
</smtp>
</mailSettings>
</system.net>


2.收件人的格式為

<abc@gmail.com> ,<def@gmail.com> ,


3.使用時要new出來喔

PublicClass Class = new PublicClass();//假如這function放在PublicClass裡時
Class.SendByMail(收件人,標題,內文, 寄件人會顯示的標題, 寄件的Email);


4.Function

/// <summary>
/// 用Client寄信 需New出來
/// </summary>
/// <param name="pMail">收件者可以,分格寄多人</param>
/// <param name="pSubject">信件標題</param>
/// <param name="pBody">信件內容</param>
/// <param name="pFromMailName">寄件人會顯示的標題</param>
/// <param name="pFromMail">寄件的Email</param>
public void SendByMail(string pMail, string pSubject, string pBody, string pFromMailName, string pFromMail)
{
System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
Mail.From = new System.Net.Mail.MailAddress(pFromMail, pFromMailName);
Mail.Subject = pSubject;
Mail.IsBodyHtml = true;
Mail.BodyEncoding = System.Text.Encoding.UTF8;
Mail.SubjectEncoding = System.Text.Encoding.UTF8;
Mail.Body = pBody;
//這邊多人可以回圈
char[] delimiterChars = { ',' };

string[] words = pMail.Split(delimiterChars);

foreach (string s in words)
{
if (s.Trim() != "")
{
Mail.To.Add(s);
}
}

System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient(System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpHost"]);
SMTPServer.Send(Mail);
System.Threading.Thread.Sleep(5);
GC.Collect();
GC.WaitForPendingFinalizers();
}

c# 去除string中重複的值

例如有一個string mStr = "1,2,3,2,4,62,24,64,1,4,75,3,2,4,75";
要去除重覆的值時可用到
string mStr = string.Join(",", mStr.Split(',').Distinct().ToArray());

c# 資料庫查詢like寫法


/// <summary>
/// 查產品
/// </summary>
/// <param name="pTitle)">產品名稱</param>
public static DataTable GetPoducts(string pTitle)
{
DataTable dt = new DataTable();

using (SqlConnection conn = new SqlConnection(ConnectionString()))
{

string Sql = "select * from Products where 1 = 1 and Title=@Title";
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = "%" + pTitle + "%";

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

cmd.Dispose();
da.Dispose();

}
return dt;
}

c# 將圖片轉為二進位

 
using System.IO;
using System.Drawing;

/// <summary>
/// 將 Image 轉換為 Byte 陣列。 /// </summary>
/// <param name="ImgPath">圖片路徑 。</param> public byte[] ImageToBuffer(string ImgPath) { byte[] _ImageBytes;
if (File.Exists(ImgPath)) {
Image _Image = Image.FromFile(ImgPath); MemoryStream ms = new MemoryStream(); _Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); _ImageBytes = ms.GetBuffer(); ms.Dispose(); _Image.Dispose(); } else { _ImageBytes = null; } return _ImageBytes; }

c# 判斷是否為數字

之前在VB中有IsNumeric可判斷是否為數字
現在換到c#時忽然沒這含數
之前在網路上找到正規表達式來判斷


using System.Text.RegularExpressions;//導入命名空間(正規表達式)

/// <summary>
/// 判斷是否為數字
/// </summary>
/// <param name="pNumber">傳入判斷的字串。</param>
public static bool IsNumeric(String pNumber)
{
Regex NumberPattern = new Regex("[^0-9.-]");
return !NumberPattern.IsMatch(pNumber);
}

T-SQL insert 後取得IDENTITY

如果上傳一筆產品+圖片時
需要抓到產品的products id後再來新增圖片所對應的 products id

在資料庫中必需要設一個識別規格(identity)


insert into abc ( ProductsName )
VALUES (ProductsName )
;SELECT @@IDENTITY;

Asp.net Web.config設定上傳檔案大小跟TimeOut的時間

近來常遇到無法上傳檔案問題
發現大概有幾個原因
問題1.資料夾權限沒設定(這當然要記得去設定)
問題2.上傳最大檔案長度超過(預設好像4mb)
問題3.上傳時間過長(預設好像90秒)

問題1的話只要進去設定權限就好
問題2 3 的話可以從web.config去修改我覺得蠻快的
在system.web標籤中加入或修改

<system.web>
<!-- 最大上傳檔案大小100MB(100*1024) TimeOut時間300秒 -->
<httpRuntime maxRequestLength="102400" executionTimeout="300"/>
</system.web>

Asp.net StreamReader 讀取純文字檔

最近常會做到一些寫固定提示語
例如公司標題之類
但是又不常修改,不常不帶表不會
為了不浪費資料庫效能所以都寫在txt裡面
這樣以後要改也可以改
string Path = Server.MapPath("txt/abcd.txt");//檔案路徑

using (StreamReader sr = File.OpenText(Path))
{
String input;
while ((input = sr.ReadLine()) != null)
{
//input如果不是null的話就會是一行文字
//做處理
}
}