2013年12月10日 星期二

繁簡轉換Function(C#)

最近有需求是若前面輸入的字串為簡體編碼則先轉換成繁體至資料庫查詢,最後結果呈現上,查出的資料庫裡的繁體資料須置換成簡體後秀出。

我找到一個很不錯的一個範例網站:http://jian-zhoung.blogspot.tw/2012/07/c.html

上述範例網站,介紹到四種方式繁簡轉換,我以這四種為例寫成一個Class檔,其他頁面統一呼叫此Class來達成。

第一步:加入一個名為「LanguageTool.cs」Class。

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Text;
   6:  
   7: namespace UDP2013
   8: {
   9:     public class LanguageTool
  10:     {
  11:  
  12:         public LanguageTool()
  13:         { 
  14:         
  15:         }
  16:     }
  17: }

第二步:加入一個判斷是否為簡體編碼的程式,名為「IsGBCode」,傳入變數為欲判斷的字串,回傳bool變數。Function如下:



   1: /// <summary>
   2: /// 判斷是否為GB2312編碼
   3: /// </summary>
   4: /// <param name="word"></param>
   5: /// <returns></returns>
   6: public bool IsGBCode(string word)
   7: {
   8:     byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
   9:     // if there is only one byte, it is ASCII code or other code
  10:     if (bytes.Length <= 1)
  11:     {
  12:         return false;
  13:     }
  14:     else
  15:     {
  16:         byte byte1 = bytes[0];
  17:         byte byte2 = bytes[1];
  18:         //判斷是否是GB2312
  19:         if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254)
  20:         {
  21:             return true;
  22:         }
  23:         else
  24:         {
  25:             return false;
  26:         }
  27:     }
  28: }

第三步:加入繁簡體轉換Function,四種方式皆一步一步列出,如下:


**Kernel32 LCMapString**


須先加入「System.Runtime.InteropServices」參考。



   1: using System.Runtime.InteropServices;

相關Function如下:



   1: #region ---使用Kernel32 LCMapString轉換---
   2:  
   3:  internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
   4:  internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
   5:  internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
   6:  
   7:  [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
   8:  internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);
   9:  
  10:  public string ToSimplified(string source)
  11:  {
  12:      String target = new String(' ', source.Length);
  13:      int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);
  14:      return target;
  15:  }
  16:  
  17:  public string ToTraditional(string source)
  18:  {
  19:      String target = new String(' ', source.Length);
  20:      int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
  21:      return target;
  22:  }
  23:  
  24:  #endregion

**Microsoft.Office.Interop.Word**


先在專案檔案中引用Com元件:Microsoft Word 14.0 Object Library。


AddMSWord


接著在程式檔案中加入「Microsoft.Office.Interop.Word」參考。



   1: using Microsoft.Office.Interop.Word;

最後附上Function,如下:



   1: /// <summary>
   2: /// 使用Microsoft.Office.Interop.Word轉換
   3: /// </summary>
   4: /// <param name="argSource"></param>
   5: /// <param name="argIsCht"></param>
   6: /// <returns></returns>
   7: public string ConvertUsingWord(string argSource, bool argIsCht)
   8: {
   9:  
  10:     var doc = new Document();
  11:     doc.Content.Text = argSource;
  12:     doc.Content.TCSCConverter(
  13:         argIsCht
  14:             ? WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC
  15:             : WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
  16:  
  17:     var ret = doc.Content.Text;
  18:     object saveChanges = false;
  19:     object originalFormat = Missing.Value;
  20:     object routeDocument = Missing.Value;
  21:     doc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
  22:  
  23:     return ret;
  24: }

**Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter**


要先在網路上下載「ChineseConverter.dll」、「ChnCharInfo.dll」參考至專案中。接著再程式檔案中加入參考。



   1: using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
   2: using Microsoft.International.Converters.PinYinConverter;

**Microsoft.VisualBasic**


首先需引用「MicrosoftVisualBasic」元件,如下。


AddMSVB


接著在程式檔案中加入參考,如下:



   1: using Microsoft.VisualBasic;

最後附上方法程式碼的部分,如下:



   1: /// <summary>
   2: /// 使用Microsoft.VisualBasic轉換
   3: /// </summary>
   4: /// <param name="argSource"></param>
   5: /// <param name="IsTraditional"></param>
   6: /// <returns></returns>
   7: public string ConvertUsingVB(string argSource, bool IsTraditional)
   8: {
   9:     string Rtnstr = "";
  10:     switch (IsTraditional)
  11:     {
  12:         case true://表示要轉換成繁體
  13:             //zh-TW (0x0404), zh-CN (0x0840), ko-KR (0x0412), en (0x0009)
  14:             Rtnstr = Strings.StrConv(argSource, VbStrConv.TraditionalChinese, 0x0404);
  15:             break;
  16:         case false://表示要轉換成簡體
  17:             //zh-TW (0x0404), zh-CN (0x0840), ko-KR (0x0412), en (0x0009)
  18:             Rtnstr = Strings.StrConv(argSource, VbStrConv.SimplifiedChinese, 0x0404);
  19:             break;
  20:     }
  21:  
  22:     return Rtnstr;
  23: }

使用方法(以第三種ChineseConverter為例):



   1: LanguageTool LanTool = new LanguageTool();
   2: string TBoxTxt = curTextBox.Text.Trim();
   3: if(LanTool.IsGBCode(curTextBox.Text.Trim()))
   4: {
   5:     TBoxTxt = LanTool.ConvertUsingCConverter(TBoxTxt,true);
   6: }

上面方法先宣告LanguageTool的物件,接著使用此物件判斷是否為簡體編碼,若是則進入判斷式中將字串轉為繁體。若轉為簡體則相反,將第二個參數設為false即可。


最後附上原始碼檔案連結


https://drive.google.com/file/d/0ByqUJS6xlP4xNkNIb3htOWdFOU0/edit?usp=sharing

2013年10月27日 星期日

ASP.NET之ValidationExpression設定

程式語言有千千百百種,但一旦使用Windows平台下的VisualStudio編輯的ASP.NET後真的會離不開它了。

因為這個IDE實在是方便,就拿表單驗證這件事來說,VS提供了方便驗證的諸多驗證控制項。只要會編輯ValidationExpression的話,往往無往不利。我找來找去,唯獨這篇文章介紹的詳細且對我來講已經是很夠用了。

參考網址:http://www.dotblogs.com.tw/wesley0917/archive/2010/12/16/20153.aspx

這篇介紹的加以組合,基本上概括大部分的驗證。

藉由這篇網誌來記錄一下ValidationExpression設定方式。

2013年10月25日 星期五

PHP連Sql Server 2008r2程式

我要來記錄一下php連Sql Server 2008 r2的方法:

參考此篇:http://www.chris.com.tw/blog/?p=294

上面連結所介紹的滿完整的,我使用他介紹的第二種方式,詳細步驟如下。

首先我先列出php及Sqlserver環境:

我使用AppServ去架php及Mysql。

  1. php版本:5.2.6
  2. Windows版本:win7(64bit)

SqlServer環境則:

  1. SqlServer版本:2008 r2
  2. Windows版本:Server 2008 r2(64bit)

第一步:要去安裝Sql Server Driver for php,下載網址:http://msdn.microsoft.com/en-us/sqlserver/cc299381.aspx

然後參考http://msdn.microsoft.com/en-us/library/cc296170.aspx去對照自己該下載的版本。

因為我使用的php是5.2.6,因此我選擇Microsoft Drivers 2.0來下。

下載下來的"SQLSRV20.EXE"點兩下便要選擇解壓縮的路徑,請選擇"C:\AppServ\php5\ext",因為php所使用到的dll檔案皆放於此。

第二步:設定php.ini,php.ini的路徑為"C:\Windows\php.ini”。可以直接用"記事本"打開此ini文件編輯,或者若是跟我一樣是用AppServ架php則可以用Windows的所有程式連結中使用AppServ提供的功能去編輯,參考下圖:

AppServConfigurephpini

編輯方式請參考:http://msdn.microsoft.com/en-us/library/cc296203.aspx,extension_dir不用再去特別設定,只要加一行"extension=php_sqlsrv_52_ts_vc6.dll"到php.ini裡extenstion設定區(以方便管理)。記得設定完後php ApacheServer要重啟。

第三步:安裝"Microsoft SQL Server 2008 R2 Native Client",參考第一步中的第二個連結中有下載網址,參考下圖:

MSNativeClient

第四步:撰寫連線程式,參考php網頁介紹的sqlsrv的各個Function:http://www.php.net/manual/en/function.sqlsrv-connect.php,我的程式用到的有:

  1. sqlsrv_connect
  2. sqlsrv_query
  3. sqlsrv_fetch_array
  4. sqlsrv_close

上面這四種就是夠用來查詢並將數據顯示,程式如下:

   1: $serverName = "localhost"; //serverName\instanceName
   2: $connectionInfo = array( "Database"=>"DBName", "UID"=>"ID", "PWD"=>"Pwd");
   3: $conn = sqlsrv_connect( $serverName, $connectionInfo);
   4:  
   5: if( $conn ) {
   6:      $sql = "SELECT Top(10) * FROM DefUser";       
   7:     $result = sqlsrv_query($conn,$sql);
   8:  
   9:     echo "<center>";
  10:     echo "<table border='1'>
  11:     <tr style='background-color: activeborder'>
  12:     <th>MEMBER_ID</th>
  13:     <th>UserName</th>
  14:     <th>DepartNo</th>
  15:     <th>me_limit</th>
  16:     </tr>";
  17:  
  18:     while($row = sqlsrv_fetch_array($result))
  19:       {
  20:       echo "<tr>";
  21:       echo "<td>" . $row['MEMBER_ID'] . "</td>";
  22:       echo "<td>" . $row['UserName'] . "</td>";
  23:       echo "<td>" . $row['DepartNo'] . "</td>";
  24:       echo "<td>" . $row['me_limit'] . "</td>";
  25:       echo "</tr>";
  26:       }
  27:     echo "</table>";        
  28:     echo "</center>";
  29: }else{
  30:      echo "Connection could not be established.<br />";
  31:      die( print_r( sqlsrv_errors(), true));
  32: }        
  33:  
  34: sqlsrv_close($conn);



第五步:執行網頁就可以看到數據顯示於網頁上,參考下圖:

WebSite