在 .NET Core 中使用异步的 ADO.NET 的简单示例
直接贴代码:
Program.cs
using Microsoft.Extensions.Configuration;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Threading.Tasks; namespace AsyncAdoNetSamples
{
class Program
{
static async Task Main()
{
await ReadAsync("Hello");
} public static async Task ReadAsync(string title)
{
var connection = new SqlConnection(GetConnectionString()); string sql = "SELECT [Title], [Publisher], [ReleaseDate] FROM [ProCSharp].[Books] WHERE lower([Title]) LIKE @Title ORDER BY [ReleaseDate]"; var command = new SqlCommand(sql, connection);
var titleParameter = new SqlParameter("Title", SqlDbType.NVarChar, );
titleParameter.Value = title;
command.Parameters.Add(titleParameter); await connection.OpenAsync(); using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection))
{
while (await reader.ReadAsync())
{
int id = reader.GetInt32();
string bookTitle = reader.GetString();
string publisher = reader[].ToString();
DateTime? releaseDate = reader.IsDBNull() ? (DateTime?)null : reader.GetDateTime();
Console.WriteLine($"{id,5}. {bookTitle,-40} {publisher,-15} {releaseDate:d}");
}
}
} public static string GetConnectionString()
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json"); IConfiguration config = configurationBuilder.Build();
return config["Data:DefaultConnection:ConnectionString"];
}
}
}
config.json
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Books;Trusted_Connection=True;"
}
}
}
.csproj
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
谢谢浏览!
在 .NET Core 中使用异步的 ADO.NET 的简单示例的更多相关文章
- .net core 中 identity server 4 之Server简单示例
Steps: 1.新建一个ASP.NET Core Web项目,SigmalHex.IdentityServer: 2.安装包 Install-Package IdentityServer4 3.St ...
- Asp.Net Core WebAPI入门整理(二)简单示例
一.Core WebAPI中的序列化 使用的是Newtonsoft.Json,自定义全局配置处理: // This method gets called by the runtime. Use thi ...
- _CrtDumpMemoryLeaks报告程序中的内存泄露问题(简单示例代码)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...
- asp.net core系列 61 Ocelot 构建服务发现简单示例
一.概述 Ocelot允许指定服务发现提供程序,如Consul或Eureka. 这二个中间件是用来实现:服务治理或秒服务发现,服务发现查找Ocelot正在转发请求的下游服务的主机和端口.目前Ocelo ...
- Spring中 aop的 xml配置(简单示例)
示例: aop,即面向切面编程,面向切面编程的目标就是分离关注点. 比如:小明(一位孩子)想吃苹果,首先得要有苹果,其次才能吃.那么妈妈负责去买水果,孩子负责吃,这样,既分离了关注点,也减低了代码的复 ...
- 在Asp.Net Core中添加区域的简单实现
使用区域,可以有效的对业务进行隔离,各种业务及分工可以更灵活.在Asp.Net Core中启用区域也是极简单的. 使用步骤: 1.在 Startup.cs 中添加区域的路由: app.UseMvc(r ...
- .net core中使用autofac进行IOC
.net Core中自带DI是非常简单轻量化的,但是如果批量注册就得扩展,下面使用反射进行批量注册的 public void AddAssembly(IServiceCollection servic ...
- .NET Core 中的日志与分布式链路追踪
目录 .NET Core 中的日志与分布式链路追踪 .NET Core 中的日志 控制台输出 非侵入式日志 Microsoft.Extensions.Logging ILoggerFactory IL ...
- .NET Core中ADO.NET SqlClient的使用与常见问题
一.简介 在很多要求性能的项目中,我们都要使用传统的ADO.NET的方式来完成我们日常的工作:目前有一些网友问有关于.NET Core操作SQL Server的问题在本文中解答一下. 本文旨在指出,在 ...
随机推荐
- Spring MVC HTTP406 Not Acceptable
今天在搞前后端分离用springmvc传递json数据的时候,第一步就卡主了,本着完事开头难的做法(哈哈哈), 报了个406?什么鬼? 百度之后发现很多人也同我一样遇到过这个问题,记录下. 找到的处理 ...
- c#日期和时间戳互转
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...
- 学习Swoole需要掌握哪些基础知识
多进程/多线程 了解Linux操作系统进程和线程的概念 了解Linux进程/线程切换调度的基本知识 了解进程间通信的基本知识,如管道.UnixSocket.消息队列.共享内存 SOCKET 了解SOC ...
- JS引擎和作用域、编译器之间对话
关于以下代码段: function foo(a) { console.log( a ); // 2 } foo( 2 ); JS引擎和作用域.编译器之间对话:
- tableView左划自定义带图片按钮
本方法实现的原理是将自定义按钮加在tableViewCell.contentView的屏幕外的frame上,打个比方,如果是5系的话,那么你自定义按钮的frame的起点就在(320+,0)(320+表 ...
- Linux—服务器SSL/TLS快速检测工具(TLSSLed)
一.下载TLSSLed [root@localhost ~]# yum install tlssled 二.服务器SSL/TLS快速检测工具TLSSLed 现在SSL和TLS被广泛应用服务器的数据加密 ...
- Could not resolve resource location pattern [classpath:com/****/mappers/*.xml]: class path resource [com/****/mappers/] cannot be resolved to URL because it does not exist的问题
这里建议先去看看路径的问题,看看application.xml的里面的导入的相应的配置文件的路径有没有问题,如下: 再者看看相应的注解有没有加上,service和controller等的注解 如果再不 ...
- 如何让table中td与四周有间距
如何让table中td与四周有间距 方法一 在td下再添加一个会计元素 <tr> <td>第2节</td> <td>语文</td> < ...
- 17.Java基础_初探类的private和public关键字
package pack1; public class Student { // 成员变量 private String name; private int age; // get/set方法 pub ...
- DRF视图功能介绍(2)
本帖最后由 杰哥,我就服你 于 2018-12-20 13:22 编辑 Django rest framework(DRF) D:是一个用于构建Web API强大又灵活的框架,基于Django框架二次 ...