zoukankan      html  css  js  c++  java
  • 数据异步

    写了这么多年的程序,相信大家都知道连接数据库少不了这几个对象,DbConnection,DbCommand,DbDataReader等等。。先来看看ContinueWith在连接数据库时嵌套过深的尴尬。

    当年异步和并发编程概念特别火,火热度参考现在的直播带货,这个时期的C#率先使用新的Task一网兜,在数据库操作的几大类中开始有了Async结尾的方法,如OpenAsync,ExecuteScalarAsync,ReadAsync 等等

    1.DB NetFramework 4.5 下 await,async的写法

     1 public static async Task<int> AsyncGetCount()
     2         {
     3             using (var connection = new MySqlConnection("server=xxx.xxx.xxx.xxx;userid=xxx;password=xxx;database=xxx;charset=utf8;port=3306;"))
     4             {
     5                 await connection.OpenAsync();
     6                 using (var command = connection.CreateCommand())
     7                 {
     8                     command.CommandText = "select count(1) from messages";
     9 
    10                     var count = await command.ExecuteScalarAsync();
    11 
    12                     Console.WriteLine($"记录条数:{count}");
    13 
    14                     return Convert.ToInt32(count);
    15                 }
    16             }
    17         }
    18 
    19 -------- output -------------
    20 
    21 记录条数:75896        

    循环写法

    public static async Task<List<string>> AsyncGetMessageList()
            {
                var messageList = new List<string>();
                using (var connection = new MySqlConnection("server=xxx.xxx.xxx.xxx;userid=xxx;password=xxx;database=xxx;charset=utf8;port=3306;"))
                {
                    await connection.OpenAsync();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "select message from messages limit 5;";
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                messageList.Add(reader["message"].ToString());
                            }
                        }
                    }
                }
                return messageList;
            }
    
    ------------ output ----------------
    你需要忘记失去的,感激拥有的,和期待将至的。
    以前的找不到了。
    对于编译错误,删除Pods文件夹然后重新pod install已经成为经验。次。
    Hello,Is there anyone here?
    放松心情
    

      https://www.cnblogs.com/huangxincheng/p/12752849.html

    并行编程中将lock锁次数降到最低实现无锁编程

    var all = filterItemList.AsParallel().Select(m => GetCustomerIDList(m)).WithDegreeOfParallelism(2).ToList();
    IEnumerable<int> final = all[0];
    all.ForEach(m => final = final.Intersect(m));
    

      

    public static async Task<List<string>> AsyncGetMessageList()
            {
                var messageList = new List<string>();
                using (var connection = new MySqlConnection("server=xxx.xxx.xxx.xxx;userid=xxx;password=xxx;database=xxx;charset=utf8;port=3306;"))
                {
                    await connection.OpenAsync();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "select message from messages limit 5;";
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                messageList.Add(reader["message"].ToString());
                            }
                        }
                    }
                }
                return messageList;
            }
    
    ------------ output ----------------
    你需要忘记失去的,感激拥有的,和期待将至的。
    以前的找不到了。
    对于编译错误,删除Pods文件夹然后重新pod install已经成为经验。次。
    Hello,Is there anyone here?
    放松心情
    

      

    publicstatic async Task<int> AsyncGetCount() { using (var connection = new MySqlConnection("server=xxx.xxx.xxx.xxx;userid=xxx;password=xxx;database=xxx;charset=utf8;port=3306;")) { await connection.OpenAsync(); using (var command = connection.CreateCommand()) { command.CommandText = "select count(1) from messages"; var count = await command.ExecuteScalarAsync(); Console.WriteLine($"记录条数:{count}"); return Convert.ToInt32(count); } } } -------- output ------------- 记录条数:75896

  • 相关阅读:
    常用的正则表达式
    Spring
    Hibernate-04
    Hibernate-03
    Hibernate-02
    Hibernate-01
    装饰器(python)
    软件工程之小组选题报告
    小组项目之需求分析与原型设计
    关于group_concat函数拼接字符超长的问题
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/12799670.html
Copyright © 2011-2022 走看看