有没有办法使用sql批量插入而不使用通用列表中的数据表?

时间:2014-03-10 20:38:04

标签: datatable bulkinsert generic-list

我有一个通用列表包含大约六百五十万。我想通过使用sql批量插入将此通用列表插入到数据库中。但是使用sql批量插入需要数据表。将通用列表转换为datatable需要花费太多时间。那么如何在不使用数据表的情况下使用sql批量插入?

修改

我使用MSSQL SERVER.NET FrameWork

我想将它与通用列表一起使用。

2 个答案:

答案 0 :(得分:0)

当然,您可以使用存储过程中的XML来批量插入数据库中的图像,此方法首先将输入数据转换为xml,然后将其保存在数据库中。 希望这会很好......

答案 1 :(得分:0)

我创建了一个继承了IDataReader的自定义datareader。我的成就如下:

public class CustomDataReader<T> : IDataReader
    {
        private readonly IEnumerator<T> list;
        private readonly List<PropertyInfo> properties = new List<PropertyInfo>();

        public CustomDataReader(IEnumerable<T> list)
        {
            this.list = list.GetEnumerator();
            var props = typeof(T).GetProperties();
            foreach (var propertyInfo in props)
            {
                properties.Add(propertyInfo);
            }
        }

        #region IDataReader Members

        public void Close()
        {
            list.Dispose();
        }

        public int Depth
        {
            get { throw new NotImplementedException(); }
        }

        public DataTable GetSchemaTable()
        {
            throw new NotImplementedException();
        }

        public bool IsClosed
        {
            get { throw new NotImplementedException(); }
        }

        public bool NextResult()
        {
            throw new NotImplementedException();
        }

        public bool Read()
        {
            return list.MoveNext();
        }

        public int RecordsAffected
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Close();
        }

        #endregion

        #region IDataRecord Members

        public int FieldCount
        {
            get { return properties.Count; }
        }

        public bool GetBoolean(int i)
        {
            throw new NotImplementedException();
        }

        public byte GetByte(int i)
        {
            throw new NotImplementedException();
        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException();
        }

        public char GetChar(int i)
        {
            throw new NotImplementedException();
        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException();
        }

        public IDataReader GetData(int i)
        {
            throw new NotImplementedException();
        }

        public string GetDataTypeName(int i)
        {
            throw new NotImplementedException();
        }

        public DateTime GetDateTime(int i)
        {
            throw new NotImplementedException();
        }

        public decimal GetDecimal(int i)
        {
            throw new NotImplementedException();
        }

        public double GetDouble(int i)
        {
            throw new NotImplementedException();
        }

        public Type GetFieldType(int i)
        {
            return properties[i].PropertyType;
        }

        public float GetFloat(int i)
        {
            throw new NotImplementedException();
        }

        public Guid GetGuid(int i)
        {
            throw new NotImplementedException();
        }

        public short GetInt16(int i)
        {
            throw new NotImplementedException();
        }

        public int GetInt32(int i)
        {
            throw new NotImplementedException();
        }

        public long GetInt64(int i)
        {
            throw new NotImplementedException();
        }

        public string GetName(int i)
        {
            return properties[i].Name;
        }

        public int GetOrdinal(string name)
        {
            throw new NotImplementedException();
        }

        public string GetString(int i)
        {
            throw new NotImplementedException();
        }

        public object GetValue(int i)
        {
            return properties[i].GetValue(list.Current, null);
        }

        public int GetValues(object[] values)
        {
            throw new NotImplementedException();
        }

        public bool IsDBNull(int i)
        {
            throw new NotImplementedException();
        }

        public object this[string name]
        {
            get { throw new NotImplementedException(); }
        }

        public object this[int i]
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }


public static void BulkInsert<T>(this IEnumerable<T> list, string connStr, string tableName)
{ 
    using (var reader = new CustomDataReader<T>(list))
    {
        using (var conn = new SqlConnection(connStr))
        {
            using (var sbc = new SqlBulkCopy(conn))
            {
                conn.Open();
                sbc.DestinationTableName = tableName;
                sbc.WriteToServer(reader);
                conn.Close();
            }
        }
    }
}