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


C# SQLiteConnection.ExecuteScalarAsync方法代码示例

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


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

示例1: AddMessage

        public async Task<UserMessage> AddMessage(string userLogin, string text)
        {
            var res = new UserMessage() {Text = text, UserLogin = userLogin, Created = DateTime.UtcNow};
            using (var conn = new SQLiteConnection(_connectionString))
            {
                await conn.OpenAsync().ConfigureAwait(false);

                int count = await conn.ExecuteAsync(
                    "insert or ignore into [Message] (Text, Created, UserId) select @text as Text, @created as Created, UserId from [User] where [Login] = @userLogin",
                        new {text, created = res.Created, userLogin}).ConfigureAwait(false);
                if (count == 0)
                {
                    int userId =
                        await
                            conn.ExecuteScalarAsync<int>(
                                "insert into [User] (Login, Created) values(@userLogin, @created); SELECT last_insert_rowid() FROM [User]",
                                new {userLogin, created = res.Created}).ConfigureAwait(false);

                    await conn.ExecuteAsync("insert into [Message] (Text, Created, UserId) values(@text, @created, @userId)", new { text, created = res.Created, userId}).ConfigureAwait(false);
                }
            }
            return res;
        }
开发者ID:alexf2,项目名称:ReactiveWebServerProto,代码行数:23,代码来源:GuestBookSqlLiteProvider.cs

示例2: GetUserMessages

        public async Task<DataPage<UserMessage>> GetUserMessages(string userLogin, int pageNumber, int pageSize)
        {
            ValidatePagination(pageNumber, pageSize);
            using (var conn = new SQLiteConnection(_connectionString))
            {
                await conn.OpenAsync().ConfigureAwait(false);

                if (await conn.ExecuteScalarAsync<int>("select count(*) from [User] where Login = @userLogin", new { userLogin }).ConfigureAwait(false) == 0)
                    return new DataPage<UserMessage>(pageNumber, pageSize, 0, null); //not found

                int count = await conn.ExecuteScalarAsync<int>("select count(*) from [Message] m join [User] u on m.UserId=u.UserId where u.Login = @userLogin", new {userLogin}).ConfigureAwait(false);

                string q = "select u.Login as UserLogin, m.Text, m.Created from [Message] m join [User] u on m.UserId=u.UserId where u.Login = @userLogin order by m.Created";
                object prms;
                if (pageSize == -1)
                    prms = new { userLogin };
                else
                {
                    q += " limit @size offset @skip";
                    prms = new { userLogin, size = pageSize, skip = (pageNumber - 1) * pageSize };
                }

                return
                    new DataPage<UserMessage>(pageNumber, pageSize, count,
                        (await conn.QueryAsync<UserMessage>(q, prms)).ToList());
            }
        }
开发者ID:alexf2,项目名称:ReactiveWebServerProto,代码行数:27,代码来源:GuestBookSqlLiteProvider.cs

示例3: GetUsers

        public async Task<DataPage<UserInfo>> GetUsers(int pageNumber, int pageSize)
        {
            ValidatePagination(pageNumber, pageSize);
            using (var conn = new SQLiteConnection(_connectionString))
            {
                await conn.OpenAsync().ConfigureAwait(false);

                int count = await conn.ExecuteScalarAsync<int>("select count(*) from [User]").ConfigureAwait(false);

                string q = "select [Login] as UserLogin, Display as DisplayName, Created from [User] order by [Login]";
                object prms;
                if (pageSize == -1)                
                    prms = null;                
                else
                {
                    q += " limit @size offset @skip";
                    prms = new { size = pageSize, skip = (pageNumber - 1) * pageSize };
                }

                return
                    new DataPage<UserInfo>(pageNumber, pageSize, count,
                        (await conn.QueryAsync<UserInfo>(q, prms).ConfigureAwait(false)).ToList());
            }
        }
开发者ID:alexf2,项目名称:ReactiveWebServerProto,代码行数:24,代码来源:GuestBookSqlLiteProvider.cs


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