English 中文(简体)
在不使用匿名物体时,应用动态参数显示“必须界定微量变量”
原标题:Dapper dynamic parameters throw a SQLException "must define scalar variable" when not using anonymous objects

(该法典正在C#中使用Dapper Dot Net)

该法典规定:

var command = "UPDATE account SET priority_id = @Priority WHERE name = @Name";
connection_.Execute(command, new { Name = "myname", Priority = 10 } );

该法典载有一项SqlException:

class MyAccount 
{
    public string Name;
    public int Priority;
}

var command = "UPDATE account SET priority_id = @Priority WHERE name = @Name";
var acct = new MyAccount { Name = "helloworld", Priority = 10 };
connection_.Execute(command, acct);

系统.Data.SqlClient.SqlException: 不得不宣布微量变量“@Priority”。

为什么?

最佳回答

1. 采用您的特性而不是领域模式:

class MyAccount 
{
    public string Name { get; set; }
    public int Priority { get; set; }
}

上诉人对物体的特性进行研究,以获取参数,而忽略田地。 匿名类型工作,因为它们是,与执行。

问题回答

虽然这一答案与海报问题有关,但我也有类似的问题,我在这里分担不同的固定办法。

我错误地将参数清单定义为<代码>new [{ }:

var name = "myName";
var priority = 1;
var command = "UPDATE account SET priority_id = @Priority WHERE name = @Name";
connection_.Execute(command, new []{ priority, name });

通过<代码>[]解决了我的问题:

connection_.Execute(command, new { priority, name });

我在数据类型方面也存在同样的问题。 带有动态物体且具有时日财产时,例外: 该成员创建类型系统。 不得将目标用作参数值。

当时我使用一个POCO,而不是后来的动态。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签