Using DataTable in .NET Core

前端 未结 6 883
你的背包
你的背包 2020-12-14 10:11

I have a stored procedure in SQL Server that accepts a User-Defined Table Type. I\'m following the answer from this post Bulk insert from C# list into SQL Server into multip

6条回答
  •  时光说笑
    2020-12-14 10:59

    Developer82,

    I am in the same situation where i want to use .net core but the unavailability of datatable, dataset is a bummer. since you are referencing a post that uses a List i thought that maybe the goal is to get the C# List into the database the cleanest way possible. If that is the goal then this might help.

    I have used Dapper located here in various projects. It is supported int .netcore. below is a small console app that takes a populated c# list and inserts it into the DB and then issues a Select on that table to writes out the results to the console.

    using System;
    using System.Data;
    using Dapper;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    
    namespace TestConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                List dataItems = GetDataItems();
    
                var _selectSql = @"SELECT CustomerId, Name, BalanceDue from [dbo].[CustomerAccount]";
    
                var _insertSql = @"INSERT INTO [dbo].[CustomerAccount]
                                           ([CustomerId]
                                           ,[Name]
                                           ,[BalanceDue])
                                     VALUES
                                           (@CustomerId
                                           ,@Name
                                           ,@BalanceDue)";
    
    
    
                using (IDbConnection cn = new SqlConnection(@"Server=localhost\xxxxxxx;Database=xxxxdb;Trusted_Connection=True;"))
                {
                    var rows = cn.Execute(_insertSql, dataItems,null,null,null );
    
                    dataItems.Clear();
    
                    var results = cn.Query(_selectSql);
    
                    foreach (var item in results)
                    {
                        Console.WriteLine("CustomerId: {0} Name {1} BalanceDue {2}", item.CustomerId.ToString(), item.Name, item.BalanceDue.ToString());
                    }
    
    
                }
    
                Console.WriteLine("Press any Key");
                Console.ReadKey();
    
            }
    
            private static List GetDataItems()
            {
                List items = new List();
    
                items.Add(new DataItemDTO() { CustomerId = 1, Name = "abc1", BalanceDue = 11.58 });
                items.Add(new DataItemDTO() { CustomerId = 2, Name = "abc2", BalanceDue = 21.57 });
                items.Add(new DataItemDTO() { CustomerId = 3, Name = "abc3", BalanceDue = 31.56 });
                items.Add(new DataItemDTO() { CustomerId = 4, Name = "abc4", BalanceDue = 41.55 });
                items.Add(new DataItemDTO() { CustomerId = 5, Name = "abc5", BalanceDue = 51.54 });
                items.Add(new DataItemDTO() { CustomerId = 6, Name = "abc6", BalanceDue = 61.53 });
                items.Add(new DataItemDTO() { CustomerId = 7, Name = "abc7", BalanceDue = 71.52 });
                items.Add(new DataItemDTO() { CustomerId = 8, Name = "abc8", BalanceDue = 81.51 });
    
                return items;
            }
        }
    }
    

    I hope this code sample helps.

    thank you.

提交回复
热议问题