/*
a、注意数据库编码要能兼容gb2312和big5,比如mysql中使用utf8
b、该代码采用遍历的方式,并用mysqlcommandbuilder进行批量更新,所以能转换的表必须包含主键,不包括主键的表则不能转换
c、引用了microsoft.visualbasic.dll进行简繁转换
*/
using system;
using system.data;
using mysql.data;
using mysql.data.mysqlclient;
using system.collections.generic;
using system.text;
using microsoft.visualbasic;
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;
}
}
}
新闻热点
疑难解答