[CSharpTips]C#读取SQLite数据库中文乱码
C#读取SQLite数据库中文乱码
C#在读取C++写入数据的Sqlite数据库中的Text内容时,会出现乱码,因为C++默认编码格式为GB2312,而Sqlite编码格式为UTF-8,存入时不统一就会出现乱码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | using System; using System.Data; using System.Data.SQLite; using System.Text; namespace SQLiteSamples { class Program { //数据库连接 SQLiteConnection m_dbConnection; static void Main( string [] args) { Program p = new Program(); //sql中使用hex(Text)将Text转换为16进制字符串 DataTable table = p.GetData( "select hex(Name) from highscores" ); var name = table.Rows[0][0].ToString(); byte [] byteresult = p.HexStringToByteArrart(name); //读取C++写入的sqlite数据库中的Text,将byte数组解析为GB2312编码字符串 //var result = Encoding.GetEncoding("GB2312").GetString(byteresult); //将byte数组解析为UTF-8编码字符串 var result = Encoding.GetEncoding( "UTF-8" ).GetString(byteresult); Console.WriteLine(result); Console.ReadLine(); } public Program() { createNewDatabase(); connectToDatabase(); createTable(); fillTable(); printHighscores(); } //创建一个空的数据库 void createNewDatabase() { SQLiteConnection.CreateFile( "MyDatabase.sqlite" ); } //创建一个连接到指定数据库 void connectToDatabase() { m_dbConnection = new SQLiteConnection( "Data Source=MyDatabase.sqlite;Version=3;" ); m_dbConnection.Open(); } //在指定数据库中创建一个table void createTable() { string sql = "create table highscores (name varchar(20), score int)" ; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //插入一些数据 void fillTable() { string sql = "insert into highscores (name, score) values ('张三', 3000)" ; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('李四', 6000)" ; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('王五', 9001)" ; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //使用sql查询语句,并显示结果 void printHighscores() { string sql = "select * from highscores order by score desc" ; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) Console.WriteLine( "Name: " + reader[ "name" ] + "\tScore: " + reader[ "score" ]); //Console.ReadLine(); } /// <summary> /// 将查询结果填充至DataTable /// </summary> /// <param name="sql"></param> /// <returns></returns> DataTable GetData( string sql) { DataTable dt = new DataTable(); lock (m_dbConnection) { SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, m_dbConnection); adapter.Fill(dt); return dt; } } /// <summary> /// 将16进制字符串转换为byte数组 /// </summary> /// <param name="input"></param> /// <returns></returns> byte [] HexStringToByteArrart( string input) { input = input.Replace( " " , "" ).Trim().ToUpper(); byte [] buffer = new byte [input.Length / 2]; for ( int i = 0; i < input.Length; i += 2) { buffer[i / 2] = ( byte )Convert.ToByte(input.Substring(i, 2), 16); } return buffer; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?