Dapper Plus - Data Source - DataTable/DataRow

CSharp · Console

/Program.cs

// Dapper Plus
// Doc: https://dapper-plus.net/data-source

// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;

public class Program
{	
	public static void Main()
	{
		// Map your entity
		DapperPlusManager.Entity<DataTable>("DataTable_Product")
			.Table("Product")
			.Map("Name")
			.Map("Description");
		
		var dtProduct = new DataTable();
		dtProduct.Columns.Add("Name", typeof(string));
		dtProduct.Columns.Add("Description", typeof(string));
		
		dtProduct.Rows.Add("Dapper Plus", @"Use <a href=""https://dapper-plus.net/"" target=""_blank"">Dapper Plus</a> to extend your IDbConnection with high-performance bulk operations.");
		dtProduct.Rows.Add("C# Eval Expression", @"Use <a href=""https://eval-expression.net/"" target=""_blank"">C# Eval Expression</a> to compile and execute C# code at runtime.");
		dtProduct.Rows.Add("Entity Framework Extensions", @"Use <a href=""https://entityframework-extensions.net/"" target=""_blank"">Entity Framework Extensions</a> to extend your DbContext with high-performance bulk operations.");

		var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
		connection.CreateTable<Product>();
		
		// DataTable/DataRow
		connection.BulkInsert("DataTable_Product", dtProduct);
		
		// Query data with Dapper
		FiddleHelper.WriteTable(connection.Query<Product>("SELECT * FROM Product"));
	}
	
	[Table("Product")]
	public class Product
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int ProductID { get; set; }
		public string Name { get; set; }
		public string Description { get; set; }
		
	}
}
;