Is query.Insert(IEnumerable <T> list) works?

46 views
Skip to first unread message

vladimir matveev

unread,
Feb 10, 2012, 10:39:18 AM2/10/12
to Business Logic Toolkit for .NET
Hi. I need some help. I tried to use this code:

using (var db = new BltDataModel())
{
var reg = new Collection<FileNames>();
reg.Add(new FileNames { FileName = "fwqffwqwf",Author
= "", bProcTime = DateTime.Now, Processed = false});
...
var query = new SqlQuery<FileNames>(db);
query.Insert(reg);
}

But it always crashes at "SqlDataProviderBase.cs" at
"bc.WriteToServer(rd)". Is it possible to use this type of
query.Insert?

vladimir matveev

unread,
Feb 14, 2012, 5:59:24 AM2/14/12
to Business Logic Toolkit for .NET
Error occurs when working with table which has an identification
column. Insert method is executed well for tables without an id.

At SqlDataProviderBase.cs at code:

foreach (var memberMapper in members)
bc.ColumnMappings.Add(new
SqlBulkCopyColumnMapping(memberMapper.Ordinal, memberMapper.Name));

"SqlBulkCopyColumnMapping" gets "memberMapper.Ordinal", but if you
have an id column in your table as first column, the first
"memberMapper.Ordinal" value will be 1, next will be 2 and etc.

So, when "WriteToServer" is executing, it calls "object GetValue(int
i)" method, which accesses to the "_members" array. This array
contains n - 1 elements, where "n - 1" - the number of columns in the
table without id. But the value of variable i in the "object
GetValue(int i)" method complies to the value "memberMapper.Ordinal".
The last value of "memberMapper.Ordinal" is "n". But "_members" array
contains "n - 1" elements. It occurs an error.
Reply all
Reply to author
Forward
0 new messages