当前位置: 首页>>代码示例>>C#>>正文


C# SQLiteConnection.CreateCollation方法代码示例

本文整理汇总了C#中SQLiteConnection.CreateCollation方法的典型用法代码示例。如果您正苦于以下问题:C# SQLiteConnection.CreateCollation方法的具体用法?C# SQLiteConnection.CreateCollation怎么用?C# SQLiteConnection.CreateCollation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SQLiteConnection的用法示例。


在下文中一共展示了SQLiteConnection.CreateCollation方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestCollation

        public void TestCollation()
        {
            var numRecords = this.rnd.Next(1, 11);

            var insertedRecords = new List<Tuple<int, string>>(numRecords);
            var queriedRecords = new List<Tuple<int, string>>(numRecords);
            var queriedRecordsReverse = new List<Tuple<int, string>>(numRecords);

            for (var i = 0; i < numRecords; i++)
            {
                insertedRecords.Add(new Tuple<int, string>(i, this.GetRandomString()));
            }

            using (var connection = new SQLiteConnection(this.databaseRelativePath))
            {
                connection.CreateCollation(
                    "CUSTOMCOLL",
                    new Collation((first, second) =>
                    {
                        return string.Compare(first, second);
                    }));

                connection.CreateCollation(
                    "CUSTOMCOLLREVERSE",
                    new Collation((first, second) =>
                    {
                        return string.Compare(second, first);
                    }));

                using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestCollation;"))
                {
                    statement.Step();
                }

                using (var statement = connection.Prepare("CREATE TABLE TestCollation(id INTEGER, t TEXT);"))
                {
                    statement.Step();
                }

                foreach (var record in insertedRecords)
                {
                    var command = "INSERT INTO TestCollation(id, t) VALUES(" + record.Item1.ToString(this.invClt) + ",'" + record.Item2 + "');";

                    using (var statement = connection.Prepare(command))
                    {
                        statement.Step();
                    }
                }

                using (var statement = connection.Prepare("SELECT id, t FROM TestCollation ORDER BY t COLLATE CUSTOMCOLL, id;"))
                {
                    while (statement.Step() == SQLiteResult.ROW)
                    {
                        var id = (long)statement[0];
                        var t = (string)statement[1];

                        queriedRecords.Add(new Tuple<int, string>((int)id, t));
                    }
                }

                using (var statement = connection.Prepare("SELECT id, t FROM TestCollation ORDER BY t COLLATE CUSTOMCOLLREVERSE, id;"))
                {
                    while (statement.Step() == SQLiteResult.ROW)
                    {
                        var id = (long)statement[0];
                        var t = (string)statement[1];

                        queriedRecordsReverse.Add(new Tuple<int, string>((int)id, t));
                    }
                }

                using (var statement = connection.Prepare("DROP TABLE TestCollation;"))
                {
                    statement.Step();
                }
            }

            Assert.AreEqual(insertedRecords.Count, queriedRecords.Count);
            Assert.AreEqual(insertedRecords.Count, queriedRecordsReverse.Count);

            insertedRecords.Sort((x, y) => { return string.Compare(x.Item2, y.Item2) != 0 ? string.Compare(x.Item2, y.Item2) : x.Item1 - y.Item1; });

            for (var i = 0; i < insertedRecords.Count; i++)
            {
                var insertedRecord = insertedRecords[i];
                var queriedRecord = queriedRecords[i];

                Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
                Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
            }

            insertedRecords.Sort((x, y) => { return string.Compare(y.Item2, x.Item2) != 0 ? string.Compare(y.Item2, x.Item2) : x.Item1 - y.Item1; });

            for (var i = 0; i < insertedRecords.Count; i++)
            {
                var insertedRecord = insertedRecords[i];
                var queriedRecord = queriedRecordsReverse[i];

                Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
                Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
//.........这里部分代码省略.........
开发者ID:matrostik,项目名称:SQLitePCL.Windows.SQLCipher,代码行数:101,代码来源:GeneralUnitTest.cs


注:本文中的SQLiteConnection.CreateCollation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。