C#轻松拖放如何从SQL创建DataTables?

时间:2017-07-05 20:25:07

标签: c# sql visual-studio console-application

在处理非常小的应用程序和sqlbulkcopy时,我通常通过在空数据表上使用FILL来创建数据表,或者我只输入类似这样的内容

DataTable dtGrps = new DataTable();
dtGrps.Columns.Add("objectGuid", typeof(Guid));
dtGrps.Columns.Add("DN", typeof(string));
dtGrps.Columns.Add("CN", typeof(string));
dtGrps.Columns.Add("groupType", typeof(string));
dtGrps.Columns.Add("description", typeof(string));
dtGrps.Columns.Add("whenCreated", typeof(string));
dtGrps.Columns.Add("whenChanged", typeof(string));

但是我发现有一种内置方式(非EF或Linq)通过一些拖放方法创建上面的所有代码。我的意思是我正在使用VS2017,肯定MS已经添加了这个功能,我错过了它就是全部。

这样存在吗?

1 个答案:

答案 0 :(得分:0)

这是一个非常粗略的实现,只是作为一个起点,因为我的目标不是编写新的库。它可以使用很多优化。但是你可以使用强类型对象,使用反射基于对象生成DataTable,然后使用SqlBulkCopy来插入它。

using System;
using System.Collections.Generic;
using System.Data;

namespace StronglyStypedSqlBulkCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Car> cars = GetSampleData();
            DataTable dataTable = ConvertToDataTable(cars);

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey(true);
        }

        public static List<Car> GetSampleData()
        {
            return new List<Car> {
                new Car { Id = 1, Make = "Toyota", Model = "Tacoma", DateOfManufacture = DateTime.Now.AddDays(-1) },
                new Car { Id = 2, Make = "Ford", Model = "Raptor", DateOfManufacture = DateTime.Now.AddDays(-2) },
                new Car { Id = 3, Make = "Ram", Model = "1500", DateOfManufacture = DateTime.Now.AddDays(-3) }
            };
        }

        public static DataTable ConvertToDataTable<T>(IEnumerable<T> objects)
        {
            var properties = objects.GetType().GetGenericArguments()[0].GetProperties();

            var table = new DataTable();

            foreach (var property in properties)
            {
                var columnName = property.Name; //may want to get from attribute also
                //probably want to define an explicit mapping of .NET types to SQL types, and allow an attribute to specifically specify the SQL type
                table.Columns.Add(columnName, property.PropertyType);
            }

            //probably want to cache the mapping from above in a real implementation

            foreach (var obj in objects)
            {
                var row = table.NewRow();

                foreach (var property in properties)
                {
                    var columnName = property.Name; //may want to get from attribute also
                    var propertyValue = property.GetValue(obj);
                    row[columnName] = propertyValue;
                }

                table.Rows.Add(row);
            }

            return table;
        }
    }

    public class Car
    {
        public int Id { get; set; }

        public string Make { get; set; }

        public string Model { get; set; }

        public DateTime DateOfManufacture { get; set; }
    }
}

一旦你有一个很好的ConvertToDataTable实现,它只是定义强类型类的问题,它比原始DataTables更容易使用。