【问题标题】:Sqlite Subsonic C#: Guid is saving as Guid with SQL, but with strange characters when using codeSqlite Subsonic C#:Guid 使用 SQL 保存为 Guid,但使用代码时字符奇怪
【发布时间】:2016-07-29 21:49:30
【问题描述】:

我正在使用带有 sqlCommand 字符串的 System.Data.SQLite,这会按预期保存一个 Guid。

使用代码我奇怪的字符保存而不是 Guid,如下所示:ù“•»I={E±gÒ §[,

似乎生成奇怪字符的代码(如 SQLite Administrator 中所示):

...
// Constructor in Class
public ProfileUserAssignment()
{
    ID = Guid.NewGuid();
    _IsNew = true;

    SetDefaults();
}
...

...
// Save Method in same class
public ProfileUserAssignment Save()
{
    if (_IsNew)
    {
        Made4Print.SQLite.Repository.Add(this);
    }
    else
    {
        Made4Print.SQLite.Repository.Update(this);
    }

    return Get(this.ID);
}
...

按预期保存 Guid 的代码:

// Create Administrator User
using (System.Data.SQLite.SQLiteConnection connection = new System.Data.SQLite.SQLiteConnection(Made4Print.SQLite.GetProvider().ConnectionString))
{
    connection.Open();
    using (System.Data.SQLite.SQLiteCommand sqlCommand = new System.Data.SQLite.SQLiteCommand(connection))
    {
        StringBuilder sqlQuery = new StringBuilder();
        sqlQuery.Append("INSERT INTO [Users] ( ");
        sqlQuery.Append("[ID], ");
        sqlQuery.Append("[FirstName], ");
        sqlQuery.Append("[LastName], ");
        sqlQuery.Append("[Username], ");
        sqlQuery.Append("[Password], ");
        sqlQuery.Append("[Email], ");
        sqlQuery.Append("[Phone], ");
        sqlQuery.Append("[MobilePhone], ");
        sqlQuery.Append("[LoginEnabledPropertyValue], ");
        sqlQuery.Append("[SendEmailsPropertyValue], ");
        sqlQuery.Append("[SystemPropertyValue] ");
        sqlQuery.Append(" ) VALUES ( ");
        sqlQuery.Append("'2bdcac4d-019f-4213-b635-86ae8f7d757e', ");
        sqlQuery.Append("'Administrator', ");
        sqlQuery.Append("'User', ");
        sqlQuery.Append("'xxxxx', ");
        sqlQuery.Append("'" + Security.HashPassword("xxxxx") + "', ");
        sqlQuery.Append("'', ");
        sqlQuery.Append("'', ");
        sqlQuery.Append("'', ");
        sqlQuery.Append("1, ");
        sqlQuery.Append("1, ");
        sqlQuery.Append("1 ");
        sqlQuery.Append(" ) ");
        sqlCommand.CommandText = sqlQuery.ToString();
        sqlCommand.ExecuteNonQuery();
    }
    connection.Close();
}

使用 SQLite Administrator,我在一个表中看到了预期的 Guid,或者在另一个表中看到了字符(没有尝试在一个表中同时进行)

发现新信息:

我刚刚在以下位置找到了这个:http://www.connectionstrings.com/sqlite

将 GUID 存储为文本 通常,GUID 以二进制格式存储。使用此连接字符串将 GUID 存储为文本。

数据源=文件名;版本=3;BinaryGUID=False;

有这方面的专家吗?

【问题讨论】:

  • 我在构造函数中使用 ID = Guid.NewGuid(),我假设它试图保存 Guid 对象而不是 Guid 字符串值?如果这有意义吗?
  • 调用的代码sn-p是什么?在代码版本中可以调用 Guid.NewGuid().ToString() 代替吗?
  • 请向我们展示所涉及的代码,以及文本“使用 SQLite 管理员,我看到了预期的 Guid 或字符?”,这是一个问题,还是您真的看到了预期的结果?
  • @GrayWizardx:添加了一些代码,我可以使用字符串作为 ID,还没有考虑过。
  • @Lasse V. Karlsen :不,这是一个声明,我都看到了。

标签: c# sqlite subsonic


【解决方案1】:

SQLite 没有我读过的“类型”的概念,所以我的第一个猜测是它正在将值转换为字符串,而你的编码让它变得疯狂。那么,第一个问题:你的数据库和你机器上的默认编码是什么?

下一步 - 它不起作用吗?有时在不要求去厨房的情况下享用香肠是个好主意。在这种情况下,SQLite 正在做一些时髦的事情,但如果它返回值 OK,那么有人可能会建议您不应该查看它们 :)。

【讨论】:

  • 我猜你是对的,它没有正确翻译一些东西,一切都是默认的,开箱即用。对 Sqlite、Subsonic 和 .net 包装器来说非常新。不幸的是,用于检索预期记录的选择查询没有返回任何内容。
【解决方案2】:

看来我需要将 ID 更改为字符串而不是 Guid。

我似乎已经为 bools 这样做了,但这样做只是为了包装 ID 是没有意义的:

public int LoginEnabledPropertyValue { get; set; }

[SubSonicIgnore]
public bool LoginEnabled
{
     get
     {
          return (LoginEnabledPropertyValue > 0 ? true : false);
     }
     set
     {
          LoginEnabledPropertyValue = (value ? 1 : 0);
     }
 }

【讨论】:

  • 在找到更好的解决方案之前,我一直在使用字符串 ID。
【解决方案3】:

如果使用没有 BinaryGUID=False 的 GUID;标记您必须使用 AddWithValue 方法构建查询, 我是这样做的:

string SQL = "select count(*) from Groups where ID = @id";

SQLiteCommand cmd = new SQLiteCommand(SQL);

cmd.Parameters.AddWithValue("@ID", groupId);

cmd.Connection = sql_con;

var ret = cmd.ExecuteScalar();

【讨论】:

    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多