Data | database | conversion
/*
A, note that the database coding to be compatible with gb2312 and Big5, such as the use of MySQL UTF8
b, the code in the way of traversal, and with Mysqlcommandbuilder for batch update, so the table can be converted must contain a primary key, not including the primary key table can not be converted
C, a reference to the Microsoft.VisualBasic.dll for simple reproduction
*/
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)
{
Entrance
Console.WriteLine ("Please enter the database location IP:");
String ip = Console.ReadLine (). Trim ();
Console.WriteLine ("Please Enter database name:");
String db = Console.ReadLine (). Trim ();
Console.WriteLine ("Please enter login database username:");
String user = Console.ReadLine (). Trim ();
Console.WriteLine ("Please enter login database password:");
String PSW = Console.ReadLine ();
String connectionString = "Data source= + IP +"; User id= "+ user +"; Password= "+ PSW +";D atabase= "+ db +"; Allow Zero datetime=true; Charset=utf8; ";
Console.WriteLine ("The generated database connection string is: {0}", continue?) (y/n) ", connectionString);
if (Console.ReadLine (). ToString (). ToUpper () = = "Y")
{
DataTable that contains all table names
DataTable Dtall = tablelist (connectionString);
if (Dtall!= null)
{
if (DtAll.Rows.Count > 0)
{
Console.Write (in the conversion, please wait: ");
for (int i = 0; i < DtAll.Rows.Count; i++)
{
Dtconvert (Dtall.rows[i][0]. ToString (), connectionString);
}
}
}
}
}
Convert each row in the DataTable to traditional
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);
Add primary Key mappings
Da. MissingSchemaAction = MissingSchemaAction.AddWithKey;
DT = new DataTable ();
Da. Fill (DT);
Traversing DT to do the substitution
if (dt. Rows.Count > 0)
{
If the table contains a primary key
if (dt. Primarykey.length > 0)
{
#region Traversal
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);
}
}
Releasing resources
Builder. Dispose ();
Cmd. Dispose ();
Da. Dispose ();
Dt. Clear ();
Dt. Dispose ();
}
catch (Exception error)
{
Console.WriteLine (Error. ToString ());
}
Finally
{
Conn. Close ();
}
}
}
//traverse each table
private static DataTable tablelist (string connectionString)
{
datatable dt = new DataTable ();
using (mysqlconnection conn = new Mysqlconnection (connectionString))
{
Show tables lists all tables for MySQL, such as SQL Server please use the relevant command
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;
}
/simplified to traditional
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;
}
}
}