使用SqlBulkCopy

时间:2019-03-07 10:52:28

标签: c# asp.net linq sqlbulkcopy

我试图使用SqlBulkCopy向数据库插入一个包含多个链接表的列表。将数据添加到数据库后,我发现表之间的链接不再存在。

以下是课程:

    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string phoneNumber { get; set; }
        public virtual ICollection<tblStudentCourses> tblStudentCourses { get; set; }
        public virtual ICollection<tblTeachers> tblStudentCourses { get; set; }
    }

    public class tblStudentCourses
    {
        public int id { get; set; }
        public int studentId {get;set}
        public string courseName {get;set}
    }
    public class tblTeachers
    {
        public int id { get; set; }
        public int studentId {get;set}
        public string teacherName {get;set}
    }


List<Student> student = new List<Student>();

这是我用来在数据库中插入列表的方法:

var allStudent = student.Select(x=> new {x.id,x.name,x.email,x.phoneNumber});
var studentCourses = student.SelectMany(x => x.tblStudentCourses.Select(y => new {x.id,x.studentId,x.courseName}))

对于上述每个查询,我都将其称为SqlBulkCopy扩展名:

public static DataTable AsDataTable<T>(this IList<T> data)
        {
            DataTable dataTable = new DataTable();
            PropertyDescriptorCollection propertyDescriptorCollection =
                TypeDescriptor.GetProperties(typeof(T));
            for (int i = 0; i < propertyDescriptorCollection.Count; i++)
            {
                PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
                Type type = propertyDescriptor.PropertyType;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                    type = Nullable.GetUnderlyingType(type);


                dataTable.Columns.Add(propertyDescriptor.Name, type);
            }
            object[] values = new object[propertyDescriptorCollection.Count];
            foreach (T iListItem in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
                }
                dataTable.Rows.Add(values);
            }
            return dataTable;
        }

数据库结果

Student :
|---------------------|------------------|------------|
|      id             |     name         |  email     |.....
|---------------------|------------------|------------|
|          1          |    Johnny        | me@me.com  |       
|---------------------|------------------|------------|


Course :
|---------------------|------------------|------------|
|      id             |     studentId    | courseName |     
|---------------------|------------------|------------|
|          1          |    0             |  JAVA      |
|---------------------|------------------|------------|

2 个答案:

答案 0 :(得分:0)

请尝试这个。

第1步:添加Newtonsoft软件包

PM> Install-Package Newtonsoft.Json

第2步:将列表转换为JSON,然后将JSON转换为数据表,而无需foreach循环。

var json = JsonConvert.SerializeObject(lst);
DataTable dtt = JsonConvert.DeserializeObject<DataTable>(json);

步骤3:在存储过程中创建用户定义的表并将多个数据表传递给SQL之后。

答案 1 :(得分:0)

请参见示例,以实现不进行foreach循环即可将列表转换为数据表。

 public class Friend
        {
            public int id { get; set; }
        }

 List<Friend> lst = new List<Friend>();
            lst.Add(new Friend{ id = 1});
            lst.Add(new Friend { id = 1 });
            lst.Add(new Friend { id = 2 });
            lst.Add(new Friend { id = 3 });
            lst.Add(new Friend { id = 3 });
            lst.Add(new Friend { id = 4 });
            var json = JsonConvert.SerializeObject(lst);
            DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);