【问题标题】:SQLiteFunction Simple Not WorkingSQLiteFunction 简单不起作用
【发布时间】:2010-12-28 23:01:38
【问题描述】:

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

System.Data.SQLite.dll 中出现“System.Data.SQLite.SQLiteException”类型的未处理异常 附加信息:“DEMOIT”附近的 SQLite 错误:语法错误

我正在使用带有 SQLite ADO.NET 1.0.65 的 .NET 3.5 x86 - 帮助!

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

谢谢!

【问题讨论】:

  • 您的 SQL 中存在语法错误。靠近DEMOIT

标签: c# sqlite ado.net function


【解决方案1】:

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

【讨论】:

  • 我仍然遇到语法错误。我已将我的代码减少到最低限度(与我原来的帖子略有不同),但仍然不行。你知道这些东西的工作样本在哪里吗? phxsoftware 论坛对 SQLiteFunction 保持“沉默”。谢谢。
  • 您好,我添加了一个示例,请查看。
猜你喜欢
  • 2014-08-05
  • 2017-09-23
  • 2021-03-11
  • 2021-05-18
  • 2018-11-14
  • 2017-05-19
  • 2013-03-21
  • 1970-01-01
相关资源
最近更新 更多