让Sqlite3以GB2312编码存储汉字
来源:岁月联盟
时间:2008-09-21
using System;using System.Text;using System.Data;using Finisar.SQLite;namespace SqliteConvert{ /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // //打开以前的库 SQLiteConnection sqliteConn = new SQLiteConnection(@"Data Source=d:projectDrEye.db;New=False;Compress=True;Version=3;"); sqliteConn.Open(); SQLiteDataAdapter sqliteDa = new SQLiteDataAdapter("select * from DrEye",sqliteConn); DataSet sqliteDs = new DataSet(); sqliteDa.Fill(sqliteDs); //创建新库 SQLiteConnection sqliteConnNew = new SQLiteConnection(@"Data Source=d:projectDrEye.new1.db;New=True;Compress=True;Version=3"); sqliteConnNew.Open(); //建表 SQLiteCommand sqliteCmdNew = sqliteConnNew.CreateCommand(); sqliteCmdNew.CommandText = "CREATE table DrEye (word TEXT Primary Key,explanation BLOB,InflectedForm BLOB)"; sqliteCmdNew.ExecuteNonQuery(); //插入数据 SQLiteDataAdapter sqliteDaNew = new SQLiteDataAdapter("select * from DrEye", sqliteConnNew); SQLiteCommandBuilder sqliteCbNew = new SQLiteCommandBuilder(sqliteDaNew); DataSet sqliteDsNew = new DataSet(); sqliteDaNew.Fill(sqliteDsNew); Console.WriteLine(sqliteDs.Tables[0].Rows.Count); Console.WriteLine(sqliteDsNew.Tables[0].Rows.Count); foreach(DataRow dr in sqliteDs.Tables[0].Rows) { DataRow sqliteDrNew = sqliteDsNew.Tables[0].NewRow(); sqliteDrNew[0] = dr[0]; sqliteDrNew[1] = EncodingConvert(dr[1].ToString()); if (dr[2].ToString() != "") { sqliteDrNew[2] = EncodingConvert(dr[2].ToString()); } sqliteDsNew.Tables[0].Rows.Add(sqliteDrNew); break; } sqliteDaNew.Update(sqliteDsNew); Console.WriteLine("Game over!"); Console.ReadLine(); } //将字符串转换成字节 static byte[] EncodingConvert(string utf8String) { byte[] ansiCode; ansiCode = Encoding.Default.GetBytes(utf8String); return ansiCode; } }}
上一篇:维基百科:GB2312编码