关键词搜索

源码搜索 ×
×

SQLServer实现快速进行简繁体的翻译功能

发布2018-07-25浏览2133次

详情内容

1  创建简繁体对照表

脚本如下:

  1. CREATE TABLE [dbo].[SYS_BGBIG](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [gb] [nvarchar](1) NULL,
  4. [big] [nvarchar](1) NULL,
  5. CONSTRAINT [PK_SYS_BGBIG] PRIMARY KEY CLUSTERED
  6. (
  7. [ID] ASC
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  9. ) ON [PRIMARY]

可以从这里下载常用简繁体对照表1500多个简繁体对照(仅有繁体字的汉字,简繁体字一样的不需要对照)

https://download.csdn.net/download/postfxj/10563082

2 也可以自行收集简繁体字对照,方法如下(c#方法):

2.1  创建简繁转换的类,不需要使用第三方dll,直接使用windows的API

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. namespace POS
  7. {
  8. class ConvertSCTC
  9. {
  10. #region 简体繁体转换
  11. //public enum ConvertType
  12. //{
  13. // Simplified,
  14. // Traditional
  15. //}
  16. [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
  17. public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
  18. public const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
  19. public const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
  20. public static Encoding gb2312 = Encoding.GetEncoding(936);
  21. public static string SCTOTCConvert(string TextValue)
  22. {
  23. if (TextValue != "")
  24. {
  25. String ReturnTextValue = "";
  26. byte[] source = gb2312.GetBytes(TextValue);
  27. byte[] dest = new byte[source.Length];
  28. LCMapString(0x0804, LCMAP_TRADITIONAL_CHINESE, source, -1, dest, source.Length);
  29. ReturnTextValue = gb2312.GetString(dest);
  30. return ReturnTextValue;
  31. }
  32. else return "";
  33. }
  34. public static string TCTOSCConvert( string TextValue)
  35. {
  36. if (TextValue != "")
  37. {
  38. String ReturnTextValue = "";
  39. byte[] source = gb2312.GetBytes(TextValue);
  40. byte[] dest = new byte[source.Length];
  41. LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, source, -1, dest, source.Length);
  42. ReturnTextValue = gb2312.GetString(dest);
  43. return ReturnTextValue;
  44. }
  45. else
  46. return "";
  47. }
  48. public static void TCTOSCConvert( Control c)
  49. {
  50. c.Text = TCTOSCConvert(c.Text );
  51. if (c is TextBox )
  52. ((TextBox)c).SelectionStart = ((TextBox)c).Text.Length;
  53. }
  54. public static void SCTOTCConvert(Control c)
  55. {
  56. c.Text = SCTOTCConvert(c.Text);
  57. if (c is TextBox)
  58. ((TextBox)c).SelectionStart = ((TextBox)c).Text.Length;
  59. }
  60. #endregion
  61. }
  62. }

2.1  收集简繁字库方法如下,以任意表任意字段为例来进行收集。

  1. private void btnSJ_Click(object sender, EventArgs e)
  2. {
  3. waiting w=new waiting ();
  4. try
  5. {
  6. w.Show ();
  7. if(dgvData .Rows .Count >0)
  8. for (int i = 0; i < dgvData.Rows.Count; i++)
  9. {
  10. string s = dgvData.Rows[i].Cells[txtField.Text].Value.ToString();
  11. for(int j=0;j<s.Length ;j++)
  12. {
  13. string sFT = s.Substring(j, 1);
  14. string sJT = ConvertSCTC.TCTOSCConvert(sFT);
  15. if (sFT != sJT)
  16. {
  17. string sql = "Select * FROM SYS_BGBIG WHERE BIG=N'" + sFT + "'";
  18. bool isData = WEBPOS.IsExit(BASEINFO.DESEncrypt(sql));
  19. if (!WEBPOS.IsExit(BASEINFO.DESEncrypt(sql)))
  20. {
  21. sql = "insert into SYS_BGBIG(GB,BIG) VALUES(N'" + sJT + "',N'" + sFT + "')";
  22. WEBPOS.SQLcmd(BASEINFO.DESEncrypt(sql));
  23. }
  24. }
  25. }
  26. }
  27. }
  28. finally
  29. {
  30. w.Close ();
  31. }
  32. }

3  在SQL Server中实现简体和繁体的转换

创建标量值函数,实现简到繁和繁到简的任意翻译。

  1. create FUNCTION [dbo].[f_GB2BIG](
  2. @str nvarchar(4000), --要转换的字符串
  3. @toBIG bit --转换标志,为,表示GB-->BIG,否则是BIG-->GB
  4. )RETURNS nvarchar(4000)
  5. AS
  6. BEGIN
  7. IF @toBIG=1
  8. SELECT @str=REPLACE(@str,gb,big)
  9. FROM SYS_BGBIG
  10. WHERE CHARINDEX(gb,@str)>0
  11. ELSE
  12. SELECT @str=replace(@str,big,gb)
  13. FROM SYS_BGBIG
  14. WHERE charindex(big,@str)>0
  15. RETURN(@str)
  16. END

使用示例 :Select dbo.f_GB2BIG(N'国华发展',1) 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载