Thứ Tư, 7 tháng 11, 2012

Hàm bỏ dấu tiếng việt trong C#

Bỏ dấu bằng cách replace các ký tự có dấu về ký tự không dấu tương ứng:
  1. public static string RemoveUnicode(string text)  
  2. {  
  3.     string[] arr1 = new string[] { "á""à""ả""ã""ạ""â""ấ""ầ""ẩ""ẫ""ậ""ă""ắ""ằ""ẳ""ẵ""ặ",  
  4.     "đ",  
  5.     "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
  6.     "í","ì","ỉ","ĩ","ị",  
  7.     "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
  8.     "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
  9.     "ý","ỳ","ỷ","ỹ","ỵ",};  
  10.     string[] arr2 = new string[] { "a""a""a""a""a""a""a""a""a""a""a""a""a""a""a""a""a",  
  11.     "d",  
  12.     "e","e","e","e","e","e","e","e","e","e","e",  
  13.     "i","i","i","i","i",  
  14.     "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
  15.     "u","u","u","u","u","u","u","u","u","u","u",  
  16.     "y","y","y","y","y",};  
  17.     for (int i = 0; i < arr1.Length; i++)  
  18.     {  
  19.         text = text.Replace(arr1[i], arr2[i]);  
  20.         text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());  
  21.     }  
  22.     return text;  
  23. }  
Viết theo kiểu Extension:
  1. public static string NonUnicode(this string text)  
  2. {  
  3.     string[] arr1 = new string[] { "á""à""ả""ã""ạ""â""ấ""ầ""ẩ""ẫ""ậ""ă""ắ""ằ""ẳ""ẵ""ặ",  
  4.     "đ",  
  5.     "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
  6.     "í","ì","ỉ","ĩ","ị",  
  7.     "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
  8.     "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
  9.     "ý","ỳ","ỷ","ỹ","ỵ",};  
  10.     string[] arr2 = new string[] { "a""a""a""a""a""a""a""a""a""a""a""a""a""a""a""a""a",  
  11.     "d",  
  12.     "e","e","e","e","e","e","e","e","e","e","e",  
  13.     "i","i","i","i","i",  
  14.     "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
  15.     "u","u","u","u","u","u","u","u","u","u","u",  
  16.     "y","y","y","y","y",};  
  17.     for (int i = 0; i < arr1.Length; i++)  
  18.     {  
  19.         text = text.Replace(arr1[i], arr2[i]);  
  20.         text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());  
  21.     }  
  22.     return text;  
  23. }  
Test:
  1. string test1 = "hành trang lập trình chấm cơm".NonUnicode();  
  2. string test2 = Functions.RemoveUnicode("hành trang lập trình chấm cơm");  

Thứ Ba, 6 tháng 11, 2012

Cách làm hiệu ứng trượt khi mở Form C# Windows Form

Hôm nay ngồi coi lại cái tool viết hồi hơn 1 năm trước, tình cờ thấy lại đoạn code hồi xưa mình làm hiệu ứng trượt khi mở form con, sẵn tiện share cách làm cho ai có hứng thú.

Kéo vào Form con 1 Time Control sau đó khai báo các biến, hàm tạo và sự kiên Form Load như sau:
  1. private int _parentTop;  
  2. private int _parentLeft;  
  3. private int _parentWidth;  
  4. private int _parentHeight;  
  5.   
  6. public FrConnection(int parentTop, int parentLeft, int parentWidth, int parrentHeight)  
  7. {  
  8.     _parentTop = parentTop;  
  9.     _parentLeft = parentLeft;  
  10.     _parentWidth = parentWidth;  
  11.     _parentHeight = parrentHeight;  
  12.     InitializeComponent();  
  13. }  
  14.   
  15. private void FrConnection_Load(object sender, EventArgs e)  
  16. {  
  17.     Left = _parentLeft - 50;// vị trí khởi tạo của Form  
  18.     Top = ((_parentTop + _parentHeight / 2)) - (Height / 2);  
  19.     timer.Start();  
  20. }  
Ở sự kiện time_Tick khai báo:
  1. private void timer_Tick(object sender, EventArgs e)  
  2. {  
  3.     Left += 50;// tốc độ trượt của Form  
  4.     if (Left >= ((_parentLeft + _parentWidth / 2) - Width / 2))  
  5.     {  
  6.         Left = ((_parentLeft + _parentWidth / 2)) - (Width / 2);  
  7.         Top = ((_parentTop + _parentHeight / 2)) - (Height / 2);  
  8.         timer.Stop();  
  9.     }  
  10. }  
Ở Form cha, khi open Form con sẽ cần truyền các tham số top,left, width, heigh của mình vào Form con:
  1. private void newToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     FrConnection fr = new FrConnection(this.Top, this.Left, this.Width, this.Height);  
  4.     fr.ShowDialog();  
  5. }  
Vị trí của Form con lúc bắt đầu khởi tạo:
 Sau đó Form sẽ trượt ra chính giữa:

Có thể tùy chỉnh quãng đường Form sẽ trượt tại sự kiện Form Load và tốc độ trượt của Form tại sự kiện time_Tick.

Thứ Hai, 5 tháng 11, 2012

Kiểm tra Hệ Điều Hành 64bit bằng C#

Để kiểm tra hệ điều hành đang chạy có phải 64bit hay không, xem thông tin trong class Enviroment.
  1. bool b = Environment.Is64BitOperatingSystem;

Nén và giải nén File trong C# .NET 4.5

Phiên bản .NET Framework 4.5 đã cung cấp thư viện hỗ trợ nén và giải nén file trong NameSpace System.IO.Compression mà các phiên bản .NET Framework trước đó chưa cung cấp.

Trước hết cần đảm bảo .NET 4.5 đã được cài đặt và add reference  System.IO.Compression.FileSystem

  1. //nén thư mục TestZipFile thành TestZipFile.zip  
  2. System.IO.Compression.ZipFile.CreateFromDirectory("C:\\Users\\Phong\\Desktop\\TestZipFile""C:\\Users\\Phong\\Desktop\\TestZipFile.zip");  
  3.   
  4. //giải nén TestZipFile.zip ra thư mục TestExtract  
  5. System.IO.Compression.ZipFile.ExtractToDirectory("C:\\Users\\Phong\\Desktop\\TestZipFile.zip""C:\\Users\\Phong\\Desktop\\TestExtract");  
Xem thêm các tính năng mới trong .NET 4.5 trên MSDN.

Thứ Sáu, 26 tháng 10, 2012

Nén và giải nén chuỗi trong C# (Compress and Decompress string C#)

Do nhu cầu công việc mình cần lưu trữ 1 nội dung rất lớn xuống database ( cụ thể là nội dung HTML của các website).
Với số lượng record lên đến hàng trăm ngàn, lượng dữ liệu lưu xuống là cực kỳ lớn, lên đến hàng chục GB. Dữ liệu thực sự của công ty là hàng triệu, có thể tưởng tưởng ra cái database để lưu trữ nó to đến mức nào ^^!.
Cho nên trước khi lưu xuống mình nén nội dung lại để giảm bớt dung lượng lưu trữ.
Cách làm này giúp tiết kiệm dung lượng lưu trữ rất lớn (Mình áp dụng test 100.000 record thì thấy database chỉ còn 1/3 so với khi chưa nén). Tuy nhiên khi cần ta không thể thực hiện các xử lý tính toán trực tiếp với dữ liệu này dưới SQL được mà cần phải lấy dữ liệu lên, giải nén ngược trở lại rồi xử lý.
  1. public static string Compress(string text)  
  2. {  
  3.     byte[] buffer = Encoding.UTF8.GetBytes(text);  
  4.     MemoryStream ms = new MemoryStream();  
  5.     using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))  
  6.     {  
  7.         zip.Write(buffer, 0, buffer.Length);  
  8.     }  
  9.   
  10.     ms.Position = 0;  
  11.     MemoryStream outStream = new MemoryStream();  
  12.   
  13.     byte[] compressed = new byte[ms.Length];  
  14.     ms.Read(compressed, 0, compressed.Length);  
  15.   
  16.     byte[] gzBuffer = new byte[compressed.Length + 4];  
  17.     System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);  
  18.     System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);  
  19.     return Convert.ToBase64String(gzBuffer);  
  20. }  
  1. public static string Decompress(string compressedText)  
  2. {  
  3.     byte[] gzBuffer = Convert.FromBase64String(compressedText);  
  4.     using (MemoryStream ms = new MemoryStream())  
  5.     {  
  6.         int msgLength = BitConverter.ToInt32(gzBuffer, 0);  
  7.         ms.Write(gzBuffer, 4, gzBuffer.Length - 4);  
  8.   
  9.         byte[] buffer = new byte[msgLength];  
  10.   
  11.         ms.Position = 0;  
  12.         using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))  
  13.         {  
  14.             zip.Read(buffer, 0, buffer.Length);  
  15.         }  
  16.   
  17.         return Encoding.UTF8.GetString(buffer);  
  18.     }  
  19. }  
Chú ý:
Đối với chuỗi dữ liệu lớn thì khi nén lại sẽ giảm kích thước rất nhiều. Trường hợp chuỗi ít hơn khoảng 300 hay 400 ký tự gì đấy (mình chưa test giới hạn) thì khi nén kết quả lại lớn hơn chuỗi ban đầu đấy (^^)!.

Thứ Hai, 22 tháng 10, 2012

Change Schema DB To DBO MS Sql Server

Lấy danh sách tất cả các table cần change Schema về dbo:
  1. SELECT 'ALTER SCHEMA dbo TRANSFER ' + SCHEMA_NAME(schema_id) + '.' + name  
  2. FROM sys.tables  
  3. WHERE schema_id != SCHEMA_ID('dbo'); 
Hoặc
  1. SELECT 'ALTER SCHEMA dbo TRANSFER ' + SCHEMA_NAME(schema_id) + '.' + name  
  2. FROM sys.objects  
  3. WHERE type='u' and schema_id != SCHEMA_ID('dbo');  
  4.       
Thực thi các câu truy vấn được tạo ra từ câu truy vấn trên.

Để change Schema cho Store Procedures thì làm tương tự (... from sys.procedures ...  hoặc ... from sys.objects where type='p' and ... ).

Xem thêm Xem danh sách các Object trong Database

Thứ Hai, 3 tháng 9, 2012

Một ví dụ mẫu đơn giản về cách sử dụng Linq to XML

  1.             XElement books = XElement.Parse(  
  2. @"<books>  
  3. <book>  
  4. <title>Pro LINQ: Language Integrated Query in C# 2010</title>  
  5. <author>Joe Rattz</author>  
  6. </book>  
  7. <book>  
  8. <title>Pro .NET 4.0 Parallel Programming in C#</title>  
  9. <author>Adam Freeman</author>  
  10. </book>  
  11. <book>  
  12. <title>Pro VB 2010 and the .NET 4.0 Platform</title>  
  13. <author>Andrew Troelsen</author>  
  14. </book>  
  15. </books>");  
  16.             var titles =  
  17.             from book in books.Elements("book")  
  18.             where (string)book.Element("author") == "Joe Rattz"  
  19.             select book.Element("title");  
  20.             foreach (var title in titles)  
  21.                 Console.WriteLine(title.Value);  
  22.             Console.ReadLine();  
  23.