将sqlite pragma编码设置为" UTF-16"在.net

时间:2015-04-01 18:03:51

标签: c# vb.net sqlite pragma

我想将SQLite的编码设置为UTF-16。我怎么能从代码中做到这一点? 可以在vb.net中使用,但我可以将c#转换为vb.net

我使用我转换为vb.net代码的这个url的数据库助手类: http://tech.just4sharing.com/Pages/ASP/How-to-use-SQLite-database-in-Visual-Studio.aspx

我试过这个vb.net代码:

      dbConnection = "Data Source=database.s3db;PRAGMA encoding = ""UTF-16"";"

但是这项工作很有用 @Dai post之后我在SQLiteDatabase帮助器类的构造函数中尝试了这个并删除了旧数据库

vb.net代码:

    dbConnection = "Data Source=database.s3db;"
    Dim cnn As New SQLite.SQLiteConnection(dbConnection)
    cnn.Open()
    Using cmd As SQLiteCommand = cnn.CreateCommand()
        cmd.CommandText = "PRAGMA encoding = ""UTF-16"";"
        cmd.ExecuteNonQuery()
    End Using
    cnn.Close()

c#c​​ode:

SQLiteConnection c = new SQLiteConnection(dbConnectioN)
c.Open()
using(SQLiteCommand cmd = c.CreateCommand()) {
cmd.CommandText = "PRAGMA encoding = \"UTF-16\"";
cmd.ExecuteNonQuery();
}
c.Close()

但是这个也很有用,管理员(SQLite的IDE)仍然很难过它在UTF-8上

1 个答案:

答案 0 :(得分:0)

SQLite PRAGMA语句被评估为SQL命令,因此不是连接字符串的一部分。这在他们的网站上有记录:

https://www.sqlite.org/pragma.html

  

PRAGMA语句是SQLite特有的SQL扩展,用于修改SQLite库的操作或查询SQLite库中的内部(非表)数据。 PRAGMA语句使用与其他SQLite命令相同的接口发出(例如SELECT,INSERT)

(强调我的)

所以要设置编码:

using(SQLiteCommand cmd = c.CreateCommand()) {
    cmd.CommandText = "PRAGMA encoding = \"UTF-16\"";
    cmd.ExecuteNonQuery();
}