2

我目前有一个 Linq to Sql 实现,它需要很长时间来进行插入,所以我试图使用紧凑的插入语句来加速它,但仍然使用 linq to sql 的执行命令。

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

string sqlStart = "INSERT INTO BusinessContacts (Guid,BusinessId,Name,Email,Address,City,State,PostalCode,Phone,URL) VALUES ";
string sqlParams = "(NewId(),1,{{{0}}},{{{1}}},{{{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);

但即使我传递了一个字符串数组,我仍然收到异常:A query parameter cannot be of type 'System.Object'.

4

1 回答 1

0

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

于 2013-02-05T16:56:59.847 回答