2009-06-28 2 views
5

userGuid = 'guid here'과 같은 것을 사용할 때 매개 변수를 사용할 때 Guides가 SQLite (0.4.8)에서 일치하도록하는 데 문제가 있습니다. 그러나 userGuid = @GuidHere은 사용하지 않습니다. 누구든지 아이디어가 있습니까?GUID가있는 SQLite 매개 변수 문제

만들기 :

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 문 (작업) :

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

DELETE 문 (작동하지 않음을) :

DELETE FROM Users WHERE UserGuid = @UserGuid 
,536,913 여기

내 문제를 보여주는 C# 프로그램입니다 :

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());      
      } 
     } 
    } 
} 

편집 :

그럼 그렇게 내가 정말 모든 GUID를 번역해야합니까 추측 AddParamWithValue이 가이 드의 16 바이트 담당자로 변환 것으로 보인다 먼저 문자열에 ... 다소 성가신.

답변

6

GUID 문자열 대신 GUID 개체가 아닌 AddWithValue 호출을 전달하십시오.

그래서 대신

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

이 작업을 수행 :

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