将可变参数传递给 db.Query 导致非法变量名称:ASP/HTML/WebMatrix

Passing variable parameter to db.Query results in illegal variable name: ASP/HTML/WebMatrix

感谢您抽出宝贵时间查看我的post。 我提供了我已阅读的内容的链接,以尝试解决此问题以及我使用的学习 material。

以下是我遇到问题的代码。我已尝试对这段代码进行各种排列以使其正常工作,我相信主要问题在于我的变量设置。我正在查询外部 Oracle 数据库。

    @{
    var db = Database.Open("DBNAME");
    var searchterm = "";
    var searchQuery = "SELECT * FROM DATABASE.DATABASETABLE WHERE UNIQUEID = @0";
    if (!Request.QueryString["searchterm"].IsEmpty()){
        searchterm = Request.QueryString["searchterm"];
                                                      }
  var selectedData = db.Query(searchQuery);
  var grid = new WebGrid(source: selectedData);  



}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Movies</title>

    </head>
    <body>
            <form method ="get">
                <label for="searchterm">Please enter you UNIQUEVALUE</label>
                <input type="text" name="searchterm" value="@Request.QueryString["searchterm"]" />
                <input type="submit" value="Search UNIQUEVALUE" />

            </form>
        <div>
                    @grid.GetHtml()
        </div>
    </body>
</html>

以下将按我希望的方式工作

@{
 var db = Database.Open("DBNAME");
        var searchterm = "";
        var searchQuery = "SELECT * FROM DATABASE.DATABASETABLE WHERE UNIQUEID = 'A00000000'";
        if (!Request.QueryString["searchterm"].IsEmpty()){
            searchterm = Request.QueryString["searchterm"];
                                                        }
      var selectedData = db.Query(searchQuery);
      var grid = new WebGrid(source: selectedData); 

}

但是当我将其更改为以下内容时,它会因以下错误而中断:

@{
        var db = Database.Open("DBNAME");
        var searchterm = "";
        var searchQuery = "SELECT * FROM DATABASE.DATABASETABLE WHERE UNIQUEID = @0";
        if (!Request.QueryString["searchterm"].IsEmpty()){
            searchterm = Request.QueryString["searchterm"];
                                                        }
      var selectedData = db.Query(searchQuery,searchterm);
      var grid = new WebGrid(source: selectedData);  


}

System.Data.OracleClient.OracleException: ORA-01036: 非法变量 name/number

Line 7:              searchQuery = "SELECT * FROM DATABASE.DATABASETABLE WHERE UNIQUEID = @0";                                            
Line 8:                                                          }
***Line 9:        var selectedData = db.Query(searchQuery,searchterm);***
Line 10:       var grid = new WebGrid(source: selectedData);  

将行修改为如下所示。即使尝试在参数字段中传递静态值也不适合我。

var selectedData = db.Query(searchQuery,"A00000000");

当我对查询进行硬编码时,我已经使用 Webmatrix 验证了此查询 returns 数据。

var searchQuery= "SELECT * FROM DATABASENAME.DATABASETABLE WHERE UNIQUE ID = 'A00000000'"
 var selectedData = db.Query(searchQuery);

我尝试了几种不同的方式来引用@0,并在设置值后将单引号添加到搜索词中。任何帮助将不胜感激!

Leaning/Research/Articles阅读:

在 Stack Overflow 上完成的搜索:

System.Data.OracleClient.OracleException: ORA-01036: 非法变量 name/number HTML ORA-01036: 非法变量 name/number\ ASP 非法变量 name/number ASP DB.Query()

在 Google 上完成的搜索:

Db.Query 参数

在 Stack overflow 上阅读的文章数:

System.Data.OracleClient.OracleException: ORA-01036: illegal variable name/number(不同syntax/language)

Constructing a good search query using system.data.oracleclient

Win32Exception (0x80004005): The wait operation timed out

Connecting html pages + Razor to database

在 google 上阅读的文章数:

What is the Equivalent of ObjectQuery.Parameters in DbQuery

https://social.msdn.microsoft.com/Forums/en-US/91c7fb6d-d1b8-4a7f-aec9-16336dbd619b/access-to-dbquery-parameters-for-logging?forum=adodotnetentityframework

学习 Material 曾经走到这一步:

http://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/displaying-data(通读本教程并积极编码。)

您提到您尝试了几种引用参数的方法。但是,这是 Oracle,他们使用 :0 进行参数化。你试过了吗

var searchQuery = "SELECT * FROM DATABASE.DATABASETABLE WHERE UNIQUEID = ':0'";

:0 参数只是将传递的字符串放入 sql 字符串中。

Oracle parameter binding