第一段為資料庫簡轉繁
第二段為文字檔繁轉簡
可參考 但是裡面的CODE 還需要調整
SQL 簡轉繁
-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
namespace EncodeTool
{
/*
a、注意資料庫編碼要能相容gb2312和big5,比如MySql中使用utf8
b、該代碼採用遍歷的方式,並用MySqlCommandBuilder進行批量更新,所以能轉換的表必須包含主鍵,不包括主鍵的表則不能轉換
c、引用了Microsoft.VisualBasic.dll進行簡繁轉換
*/
namespace Gb2312ToBig5
{
class Program
{
static void Main(string[] args)
{
//入口
Console.WriteLine("請輸入資料庫所在IP:");
string ip = Console.ReadLine().Trim(); Console.WriteLine("請輸入資料庫名稱:");
string db = Console.ReadLine().Trim(); Console.WriteLine("請輸入登錄資料庫使用者名:");
string user = Console.ReadLine().Trim(); Console.WriteLine("請輸入登錄資料庫密碼:");
string psw = Console.ReadLine(); string connectionString = "Data Source=" + ip + ";User ID=" + user + ";Password=" + psw + ";DataBase=" + db + ";Allow Zero Datetime=true;Charset=utf8;"; Console.WriteLine("生成的資料庫連接字串為:{0},繼續嗎?(Y/N)", connectionString);
if (Console.ReadLine().ToString().ToUpper() == "Y")
{
//包含所有表名稱的DataTable
DataTable dtAll = tableList(connectionString);
if (dtAll != null)
{
if (dtAll.Rows.Count > 0)
{
Console.Write("轉換中,請稍候:");
for (int i = 0; i < dtAll.Rows.Count; i++)
{
dtConvert(dtAll.Rows[i][0].ToString(), connectionString);
}
}
}
}
} //將DataTable中每行每列轉為繁體
private static void dtConvert(string dtName, string connectionString)
{
string sql = "";
MySqlCommand cmd = null;
MySqlDataAdapter da = null;
DataTable dt = null;
MySqlCommandBuilder builder = null; using (MySqlConnection conn = new MySqlConnection(connectionString))
{
try
{
sql = "select * from " + dtName;
cmd = new MySqlCommand(sql, conn);
conn.Open();
da = new MySqlDataAdapter(cmd);
//添加主鍵映射
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dt = new DataTable();
da.Fill(dt); //遍歷dt做替換
if (dt.Rows.Count > 0)
{
//如果表包含主鍵
if (dt.PrimaryKey.Length > 0)
{
#region 遍歷
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Columns[j].DataType.ToString() == "System.String")
{
if (dt.Rows[i][j] != null)
{
if (dt.Rows[i][j].ToString() != string.Empty)
{
dt.Rows[i][j] = getBig5(dt.Rows[i][j].ToString());
Console.Write(".");
}
}
}
}
}
#endregion builder = new MySqlCommandBuilder(da);
da.Update(dt);
}
}
//釋放資源
builder.Dispose();
cmd.Dispose();
da.Dispose();
dt.Clear();
dt.Dispose();
}
catch (Exception error)
{
Console.WriteLine(error.ToString());
}
finally
{
conn.Close();
}
}
} //遍歷每個表
private static DataTable tableList(string connectionString)
{
DataTable dt = new DataTable(); using (MySqlConnection conn = new MySqlConnection(connectionString))
{
//SHOW TABLES為MySQL列出所有表,如SQLServer請使用相關命令
MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); try
{
conn.Open();
da.Fill(ds, "temp_tables");
dt = ds.Tables["temp_tables"];
}
catch (Exception error)
{
Console.WriteLine(error.ToString());
}
finally
{
conn.Close();
}
} return dt;
} //簡體轉繁體
private static string getBig5(string gb2312)
{
string big5 = "";
if ((gb2312 != null) && (gb2312 != String.Empty))
{
gb2312 = gb2312.Trim();
big5 = Strings.StrConv(gb2312, VbStrConv.TraditionalChinese, 0);
}
return big5;
}
}
}
}
文字檔 繁轉簡
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace EncodeTool
{
public class EncodeTool
{
[DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
public static void Main(String[] args)
{
if (args.Length < 1)
{
Console.WriteLine("请指定路径!");
}
String[] files = Directory.GetFiles(args[0]);
if (args.Length == 2)
files = Directory.GetFiles(args[0], args[1]);
String dir = args[0] + "\\conv";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
for (int i = 0; i < files.Length; i++)
{
StreamReader sr = new StreamReader(files[i], Encoding.GetEncoding("big5"));
String lines = sr.ReadToEnd();
sr.Close();
lines = ConvertString(lines);
//Console.WriteLine(lines);
StreamWriter sw = new StreamWriter(@"D:\EncodeTool\conv\test.txt", false, Encoding.GetEncoding("gb2312"));
//StreamWriter sw = new StreamWriter(dir + "\\" + files[i], false, Encoding.GetEncoding("gb2312"));
sw.WriteLine(lines);
sw.Close();
Console.WriteLine("转换 {0} ok!", files[i]);
}
}
public static String ConvertString(String lines)
{
Encoding gb2312 = Encoding.GetEncoding(936);
byte[] src = gb2312.GetBytes(lines);
byte[] dest = new byte[src.Length];
LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, src, -1, dest, src.Length);
return gb2312.GetString(dest);
}
}
}
沒有留言:
張貼留言