• EFCore使用SQL语句


     执行非查询原生SQL

                    string msg = "内容";
                    await db.Database.ExecuteSqlInterpolatedAsync($"delete from comments where title={msg}");

    实体相关的查询SQL ,

    FromSqlInterpolated返回结果是IQueryable类型,在执行之前还可以对其进一步处理
                    string key = "%中%";
                    var list=db.Comments.FromSqlInterpolated($"select * from comments where content like {key}");
                    foreach (var item in list.take(10))
                    {
                        Console.WriteLine($"{item.Id}:{item.Content}");
                    }

    执行任意的SQL

                    var conn = db.Database.GetDbConnection();
                    if (conn.State != System.Data.ConnectionState.Open)
                    {
                        await conn.OpenAsync();
                    }
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select id,标题 title from articles";
                        using (var reader = await cmd.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                var id = reader.GetInt32(0);
                                var title = reader.GetString(1);
                                Console.WriteLine($"{id}:{title}");
                            }
                        }
                    }

    使用Dapper

                    var conn = db.Database.GetDbConnection();
                    var list=conn.Query<OrgUnit>("select * from orgunits");
                    foreach (var item in list)
                    {
                        Console.WriteLine($"{item.Id}:{item.Name}");
                    }
  • 相关阅读:
    Apache 虚拟主机 VirtualHost 配置
    ajax无线级刷新
    Apache中 RewriteRule 规则参数介绍
    用户注册
    用户登录
    Android 侧滑菜单
    PhontoShop CS6 视频
    ArcGis地图
    Android 汉子转换成拼音
    UTF-8 转 GBK
  • 原文地址:https://www.cnblogs.com/lunawzh/p/15511104.html
Copyright © 2020-2023  润新知