IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
C#
C#

Getting data from Dapper result

Dapper query result returns DapperRow collections: IEnumerable<dynamic>{List<Dapper.SqlMapper.DapperRow>}. In order to use the data inside DapperRow object, we need to convert the data into usable types. Get single value var data = conn.ExecuteScalar("select top 1 title from book"); string title = $"{data}"; int id = conn.ExecuteScalar<int>("select top 1 id from book"); Get data from a single row var row = conn.QuerySingle("select top 1 title, url_target from book"); var title = $"{row.title}"; Get data as a Array: var data = conn.Query("select title, url from book").Select(x => x.title).ToArray(); foreach(var d in data){ string title = d; } Get data as a List: var data = conn.Query("select title, url from book").Select(x => x.title).ToList(); foreach(var d in data){ string title = d; } Get data as a Dictionary: var data = conn.Query("select title, url from book").ToDictionary(row => (string)row.title, row => (string)row.url); foreach(var k in data.Keys){ string key = k; string value = data[k]; } Get data as an object: var books = conn.Query<Book>("select * from book"); //returns IEnumerable<Book> foreach(var book in books){ string title = book.Title; } Dapper automatically mapped the column data to the fields in the object by their names.  610 total views,  5 views today

2022-08-12 0 Comments 122 Views 0 Like IT Team Read more
C#

Checks if the remote file exists

Language: C# /// <summary>Get remote file information</summary> /// <param name="url">The URL of the remote file</param> /// <returns>RemoteFile if the remote file exists, or null</returns> public static RemoteFile RemoteFileInfo(string url) { RemoteFile file = new RemoteFile(); try { var request = WebRequest.Create(url); request.Method = "HEAD"; using (var response = request.GetResponse() as HttpWebResponse) { file.LastModified = response.LastModified; file.Size = response.ContentLength; file.StatusCode = response.StatusCode; file.ContentType = response.ContentType; file.ContentEncoding = response.ContentEncoding; } } catch(Exception e) { return null; } return file; } public class RemoteFile { public DateTime LastModified { get; set; } public long Size { get; set; } public HttpStatusCode StatusCode { get; set; } public string ContentType { get; set; } public string ContentEncoding { get; set; } }  2,966 total views

2020-11-19 0 Comments 776 Views 1 Like IT Team Read more
Chinese (Simplified) Chinese (Simplified) Chinese (Traditional) Chinese (Traditional) English English French French German German Japanese Japanese Korean Korean Russian Russian
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
Common Regular Expressions Unicode to Chinese conversion notes Crawler - get data from unwrapped html content 手机邮件设定 Apostrophe 2 Theme, the image on page is not tidy up. Restrict WordPress user access some contents
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com