SQLiteFunction简单不起作用

时间:2009-12-22 01:32:24

标签: c# sqlite ado.net function

我试图使用C#和ADO.NET代码中的SQLiteFunction。谁能说我为什么会遇到这个问题?

System.Data.SQLite.dll中发生未处理的“System.Data.SQLite.SQLiteException”类型异常 其他信息:“DEMOIT”附近的SQLite错误:语法错误

我使用.NET 3.5 x86和SQLite ADO.NET 1.0.65 - 帮助!

public class Program
    {
        static void Main( string[ args )
        {
            test();
        }


        public static void test()
        {
            SQLiteConnection sqlConn = new SQLiteConnection( "Data Source=TestFoods.db;" );
            sqlConn.Open();
            SQLiteCommand sqlCmd = new SQLiteCommand( "PRAGMA integrity_check" , sqlConn);
            sqlCmd.ExecuteNonQuery();
            SQLiteFunction.RegisterFunction( typeof(DEMOIT) );
            sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where Foods.Name DEMOIT '$butter' " , sqlConn );
            sqlCmd.CommandType = CommandType.Text;
            SQLiteDataAdapter liteAdapter = new SQLiteDataAdapter( sqlCmd );
            DataSet dataSet = new DataSet();
            liteAdapter.Fill( dataSet , "Foods" );
        }

    }

    [SQLiteFunction( Name = "DEMOIT" , Arguments = 1 , FuncType = FunctionType.Scalar )]
    public class DEMOIT : SQLiteFunction
    {
        public override object Invoke( object[] args )
        {
            return Convert.ToString( args[0] ) ;
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:6)

DEMOIT是一个函数,但您正在使用它,就好像它是一个运算符。 试试这个:

sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where Foods.Name = DEMOIT('$butter')" , sqlConn );

或:

sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where DEMOIT(Foods.Name) = '$butter'" , sqlConn );

旧项目http://war3share.codeplex.com/中的示例:

SQL:

select replayHash from customData where key='Rating' and String2Int(value) < 8

代码:

using System.Data.SQLite;

namespace War3Share.Client.DAL
{
    [SQLiteFunction(Arguments = 1, FuncType = FunctionType.Scalar, Name = "String2Int")]
    class String2Int : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            string s = args[0] as string;
            return int.Parse(s);
        }
    }
}