指南的SQLite参数问题 [英] SQLite Parameter Issue with Guids

查看:68
本文介绍了指南的SQLite参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用参数时,我在使Guid在SQLite(0.4.8)中匹配时遇到问题,当我使用类似 userGuid ='guid here'的方法时,但是 userGuid = @GuidHere 不会。有人有什么想法吗?

I am having issues with getting Guids to match in SQLite (0.4.8) when using parameters, when I use something like userGuid = 'guid here' it works, but userGuid = @GuidHere it does not. Anyone have any ideas?

创建:

CREATE TABLE Users
(
   UserGuid TEXT PRIMARY KEY NOT NULL, 
   FirstName TEXT, 
   LastName TEXT
)

样本数据:

INSERT INTO Users (UserGuid, FirstName, LastName) 
VALUES ('e7bf9773-8231-44af-8d53-e624f0433943', 'Bobby', 'Bobston')

删除语句(工作中):

DELETE FROM Users WHERE UserGuid = 'e7bf9773-8231-44af-8d53-e624f0433943'

删除语句(无效):

DELETE FROM Users WHERE UserGuid = @UserGuid

一个显示我的问题的C#程序:

Here is a C# program showing my issue:

using System;
using System.Data.SQLite;

namespace SQLite_Sample_App
{
    class Program
    {
        static void Main(string[] args)
        {
            Do();
            Console.Read();
        }

        static void Do()
        {
            using(SQLiteConnection MyConnection = new SQLiteConnection("Data     Source=:memory:;Version=3;New=True"))
            {
                MyConnection.Open();
                SQLiteCommand MyCommand = MyConnection.CreateCommand();
                MyCommand.CommandText = @"
                    CREATE TABLE Users
                    (
                       UserGuid TEXT PRIMARY KEY NOT NULL, 
                       FirstName TEXT, 
                       LastName TEXT
                    );

                    INSERT INTO Users (UserGuid, FirstName, LastName) 
                    VALUES ('e7bf9773-8231-44af-8d53-e624f0433943', 'Bobby', 'Bobston');
                    ";
                MyCommand.ExecuteNonQuery();

                MyCommand.CommandText = "SELECT Count(*) FROM Users WHERE UserGuid = 'e7bf9773-8231-44af-8d53-e624f0433943'";
                Console.WriteLine("Method One: {0}", MyCommand.ExecuteScalar());

                MyCommand.Parameters.AddWithValue("@UserGuid", new Guid("e7bf9773-8231-44af-8d53-e624f0433943"));
                MyCommand.CommandText = "SELECT Count(*) FROM Users WHERE UserGuid = @UserGuid";
                Console.WriteLine("Method Two: {0}", MyCommand.ExecuteScalar());                    
            }
        }
    }
}

编辑:

似乎AddParamWithValue转换为Guid的16byte rep,所以我想我真的必须首先将所有Guid转换为字符串...有点烦人。

Well it seems that the AddParamWithValue translates to a 16byte rep of a Guid so I guess I really do have to translate all guids to strings first... kinda annoying.

推荐答案

尝试仅将GUID的字符串传递给您的AddWithValue调用,而不是GUID对象。

Try just passing the string of the GUID to your AddWithValue call, rather than the GUID object.

因此而不是

MyCommand.Parameters.AddWithValue(
    "@UserGuid", new Guid("e7bf9773-8231-44af-8d53-e624f0433943"));

执行此操作:

MyCommand.Parameters.AddWithValue(
    "@UserGuid", "e7bf9773-8231-44af-8d53-e624f0433943");

这篇关于指南的SQLite参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆