LinqToSql动态插入

时间:2013-02-05 16:29:25

标签: c# linq-to-sql

我目前有一个Linq to Sql实现,需要很长时间才能完成插入,因此我尝试使用紧凑的insert语句来加速它,但仍然使用linq来执行sql的执行命令。

我有一个string[],我试图像这样传递给ExecuteCommand的object[] parameters参数。

string sqlStart = "INSERT INTO BusinessContacts (Guid,BusinessId,Name,Email,Address,City,State,PostalCode,Phone,URL) VALUES ";
string sqlParams = "(NewId(),1,{{{0}}},{A query parameter cannot be of type 'System.Object'.},{{{2}}},{{{3}}},{{{4}}},{{{5}}},{{{6}}},{{{7}}}),";
string[] values = new string[8000];
int curParam = -1;
sbSqlValues.Append(sqlStart);
foreach (var email in emails) {
    sbSqlValues.AppendFormat(sqlParams, ++curParam, ++curParam, ++curParam, ++curParam, ++curParam, ++curParam, ++curParam, ++curParam);
    curParam -= 8;
    values[++curParam] = email.Name;
    values[++curParam] = (email.Email);
    values[++curParam] = (email.Address);
    values[++curParam] = (email.City);
    values[++curParam] = (email.State);
    values[++curParam] = (email.PostalCode);
    values[++curParam] = (email.Phone);
    values[++curParam] = (email.URL);
}
sbSqlValues.Remove(sbSqlValues.Length - 1, 1);
inserted += ExecuteCommand(sbSqlValues.ToString(), values);

但即使我传入一个字符串数组,我仍然得到例外:{{1}}

1 个答案:

答案 0 :(得分:0)

尝试:(object[])values作为第二个参数。 C#编译器将string[]转换为单元素params