Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord 未能批量插入Subsonic3,错误为“必须声明标量变量…”_Activerecord_Subsonic3 - Fatal编程技术网

Activerecord 未能批量插入Subsonic3,错误为“必须声明标量变量…”

Activerecord 未能批量插入Subsonic3,错误为“必须声明标量变量…”,activerecord,subsonic3,Activerecord,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 我正在使用主动记录模式将多个产品插入表产品。如果我一行一行地插入行,可以调用一个product.Add或调用insert.Execut

我遇到了一个问题,关于在一个批次中插入多行,使用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
我正在使用主动记录模式将多个产品插入表产品。如果我一行一行地插入行,可以调用一个product.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\U ProductName。 必须声明标量变量@ins\U ProductName。


请给我一些帮助来解决这个问题。非常感谢。

我也遇到了这个问题。如果您查看它试图运行的查询,您会看到它正在执行类似的操作这不是实际的代码,但您会明白这一点:

exec_sql N'insert into MyTable (SomeField) Values (@ins_SomeField)',N'@0 varchar(32)','@0=SomeValue'
出于某种原因,它在查询中使用@ins_+FieldName定义参数,但随后将参数作为序号传递。我还没有确定为什么/什么时候它会这样做,但在这个开发周期中,我已经浪费了足够的时间来尝试和正确诊断亚音速的问题

我实施的解决方案包括从github下载3.0.0.4源代码,并在Insert.cs的第179行进行更改

上面写着

ParameterName = _provider.ParameterPrefix + "ins_" + columnName.ToAlphaNumericOnly(),
改成

ParameterName = _provider.ParameterPrefix + Inserts.Count.ToString(),
似乎对我起了作用。我对本解决方案不作任何明示或暗示的保证。它确实对我有用,但你的里程数可能会有所不同

我还应该注意到,update语句在第181行和第194行的update.cs中也有类似的逻辑,但我还没有遇到这些问题。。。然而


老实说,我不认为亚音速已经为黄金时间做好了准备,这是一个耻辱,因为我真的很喜欢罗伯的设置方式。这就是说,我的产品现在无论好坏都有它的存在,所以您可以充分利用您所拥有的。

我已经上传了示例代码来说明这个问题