Dapper.net Oracle参数 [英] Dapper.net Oracle parameter

查看:306
本文介绍了Dapper.net Oracle参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Dapper.net与Oracle一起使用.

I am trying to use Dapper.net with Oracle.

从这篇帖子中我了解到我们可以使用不带前缀的参数,然后使用dapper适用于sql serveroracle

From this post i understood that we could use parameters with no prefixes and dapper would then work for both sql serverand oracle

在没有显式的oracle命名参数前缀:

I'm having a hard time making it to work without the explicit oracle named parameters prefix :

以下查询

sqlConnection.Query("Select * FROM document WHERE id = param1", new { param1 = 963 });

投掷ORA-00904: "PARAM1": invalid identifier

如果我尝试使用@前缀,则会抛出ORA-00936: missing expression

If i try with the @ prefix it throws ORA-00936: missing expression

如果我使用:前缀,它将按预期工作.但是我不希望我的查询依赖(尽可能)在Oracle或Sql Server上.

If i use the : prefix it works as expected. But i do not want my queries to be dependent (as far as possible) upon Oracle or Sql Server.

我正在使用最新的nuget软件包版本Dapper.dll 1.12.1.1

I am using the latest nuget package version Dapper.dll 1.12.1.1

我在做什么错或者我误解了这个帖子?

What am I doing wrong or did i misunderstand this post?

推荐答案

是的,您误解了该帖子. SQL是按原样传递的,并且必须包含正确的:param1@param1等.完全没有前缀 "是指您不使用的代码请参见-具体来说,请确保代码确实(通过某种机制):

Yes, you misunderstood the post. The SQL is passed through as-is, and must contain the correct :param1 or @param1 etc. The "no prefix at all" is talking about the code that you don't see - specifically, making sure that the code does (via some mechanism):

cmd.Parameters.Add("param1", 963);

vs

cmd.Parameters.Add("@param1", 963);

vs

cmd.Parameters.Add(":param1", 963);

第一个(无前缀)是正确的首选选项.

The first (no prefix) is the correct and preferred option.

如果您希望代码中的SQL与参数无关,则可以使用以下信息:

If you want the SQL in your code to be parameter agnostic, you could use the information from here: Get the parameter prefix in ADO.NET

SQL很少足够接近,但是,仅查找参数前缀即可解决所有问题.

The SQL is rarely close enough, however, that just looking up the parameter prefix will fix all problems.

这篇关于Dapper.net Oracle参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆