在C#中创建SQLite自定义函数

Sometime we want to make user defined function and use itin our query to the database. In Ms. SQL Server and Oracle DBMS support userdefined function, stored procedure and transactional. Since SQLite is zeroconfiguration, serverless and single database file, SQLite doesn’t support userdefined function (If you don’t know what is SQLite and how to use SQLiteADO.Net with C# please refer to thispost). User defined function can be made outside of the SQLite that meansyou make custom function with your own programming language and later thatfunction can be binded to SQLite core function.

SQLite ADO.Net full support user defined function thatmeans we can make our user defined function in C# language and later we can usethe library from SQLite ADO.Net to bind it to SQLite core function then we cancall that function in our query to the database. To make a user definedfunction in SQLite ADO.Net with C# is not hard, you just need a class whichderived from SQLiteFunctionClass and override Invokefunction. See the example below:

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

That’s it, when you compile and run your application,SQLite ADO.Net will automatically bind that user defined function to SQLiteCore function. And then you can use that function in your query without aproblem.

If you have any other tricks or tips about SQLite ADO.Netplease feel free to leave a comment. Thanks and have a nice day!

你可能感兴趣的:(在C#中创建SQLite自定义函数)