SQLite没有在MonoDroid和MonoTouch上正确存储GUID值

时间:2013-04-22 05:29:37

标签: c# sqlite ado.net xamarin.ios xamarin.android

我正在使用MonoDroid将数据层从我的Window Mobile应用程序移植到Android和iOS。 我的数据层在SQL SERVER,SQL CE和Windows SQLite上运行良好且经过了很好的测试。

我在Android解决方案中为我的项目创建了类库并导入了我的所有代码。 我创建了一个新的DbEngine实现来调整Mono上SQLite的查询。

当我通过文本插入表格时,插入[用户]值(' xx',' hello',null)'它工作正常,我可以看到受影响的行> 0。 但是,当我尝试使用命令和参数插入时,我在GUID列中得到的值不正确,请参阅屏幕截图。

// Creating the parameter
public override DbParameter CreateParameter(string name, object value)
{
    DbParameter result;
    if (value is byte[])
    {
        var length = (value as Array).Length;
        result = new SqliteParameter(name, DbType.Binary, length) { Value = value};
    }
    else if (value is string && ((string)value).Length > 4000)
    {
        var length = ((string)value).Length;
        result = new SqliteParameter(name, DbType.String, length) { Value = value };
    }
    else if (value is Guid)
    {
        result = new SqliteParameter(name, DbType.Guid) { Value = value }; // up to here, I can see that the GUID has the right value and the SQLiteParameter is created properly. 
    }
    else
    {
        result = new SqliteParameter(name, value);
    }
    return result;
}

这是作为参数传递时插入行的方式 enter image description here

1 个答案:

答案 0 :(得分:3)

经过长时间的努力,似乎GUID不支持Mono.Data.SQLite个参数。 我需要解决的问题是在我的代码中创建参数时我检查它是否为GUID,我创建类型为string的参数,如下所示:

result = new SqliteParameter(name, value.ToString());

这样可以正常工作并正确存储值,并且也可以用于查询数据库。 这非常令人沮丧,特别是当Xamarin团队告诉我这是一个Unicode问题时:( 希望它能帮助别人。