我遇到了有關使用Subsonic3批量插入多行的問題。我的開發環境包括:無法在Subsonic3中批量插入,錯誤爲「必須聲明標量變量...」
1. Visual Studio 2010, but use .NET 3.5
2. Active Record Mode in SubSonic 3.0.0.4
3. SQL Server 2005 express
4. Northwind sample database
我正在使用Active Reecord模式將多個「Product」插入表「Products」中。如果我逐一插入行,可以調用「aProduct.Add()」或調用「Insert.Execute()」多次(就像下面的代碼一樣),它可以正常工作。
private static Product[] CreateProducts(int count)
{
Product[] products = new Product[count];
for (int index = 0; index < products.Length; ++index)
{
products[index] = new Product
{
ProductName = string.Format("cheka-test-{0}", index.ToString()),
Discontinued = (index % 2 == 0),
};
}
return products;
}
private static void SucceedByMultiExecuteInsert()
{
Product[] products = CreateProducts(2);
// -------------------------------- prepare batch
NorthwindDB db = new NorthwindDB();
var inserts = from prod in products
select db.Insert.Into<Product>(x => x.ProductName, x => x.Discontinued).Values(prod.ProductName, prod.Discontinued);
// -------------------------------- batch insert
var selectAll = Product.All();
Console.WriteLine("--- before total rows = {0}", selectAll.Count().ToString());
foreach (Insert insert in inserts)
insert.Execute();
Console.WriteLine("+++ after inserting {0} rows, now total rows = {1}",
products.Length.ToString(), selectAll.Count().ToString());
}
,但如果我用 「BatchQuery」 像下面的代碼,
private static void FailByBatchInsert()
{
Product[] products = CreateProducts(2);
// -------------------------------- prepare batch
NorthwindDB db = new NorthwindDB();
BatchQuery batchquery = new BatchQuery(db.Provider, db.QueryProvider);
var inserts = from prod in products
select db.Insert.Into<Product>(x => x.ProductName, x => x.Discontinued).Values(prod.ProductName, prod.Discontinued);
foreach (Insert insert in inserts)
batchquery.Queue(insert);
// -------------------------------- batch insert
var selectAll = Product.All();
Console.WriteLine("--- before total rows = {0}", selectAll.Count().ToString());
batchquery.Execute();
Console.WriteLine("+++ after inserting {0} rows, now total rows = {1}",
products.Length.ToString(), selectAll.Count().ToString());
}
那麼它失敗,出現異常: 「 未處理的異常信息:System.Data.SqlClient.SqlException:必須聲明標量變量 「@ins_ProductName」。 必須聲明標量變量 「@ins_ProductName」。 「
請給我一些幫助來解決這個問題。非常感謝。
我已經上傳示例代碼來說明這個問題在http://github.com/andymeadows/SubSonic-Defect-Help – andymeadows 2010-07-13 04:06:15