.net core dapper (2)

蹲街弑〆低调 提交于 2019-12-05 18:05:39

0.0  今天还是简答的更一篇

  人生的一须尽欢,莫使金樽空对月

  1. 简答的插入数据,然查询整个表

 1  public static void Main(string[] args)
 2         {
 3             var connectionString = "Server=.;Database=Demo;User Id=sa;Password = keno;";
 4 
 5             //02 insert 
 6             var customerModel = new Customer { CustomerName = "张三", Address = "北京", City = "北京", Country = "中国", PostalCode = "121212" };
 7             using (var connection = new SqlConnection(connectionString))
 8             {
 9                 string sql = "insert into [dbo].[Customer]([CustomerName],[Address],[City],[PostalCode],[Country]) values(@CustomerName,@Address,@City,@PostalCode,@Country)";
10                 var addRow = connection.Execute(sql, customerModel);
11                 Console.WriteLine(addRow);
12 
13                 var customers = connection.Query<Customer>("select * from [dbo].[Customer]").ToList();
14 
15                 foreach (var customer in customers)
16                 {
17                     Console.WriteLine("id: " + customer.CustomerID + " CustomerName:  " + customer.CustomerName + " Address: " + customer.Address + " City: " + customer.City + " PostalCode: " + customer.PostalCode);
18                 }
19             }
20 
21             Console.ReadKey();
22         }

显示结果

 

2.如果将强类型去掉他的结果会变成一个动态类型

1 var customers = connection.Query("select * from [dbo].[Customer]").ToList();

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!