CSHTML - SQL QUERY .... WHERE NAME = @ NameOne

时间:2011-07-22 22:24:35

标签: asp.net razor

我使用的是ASP.NET Razor。

<form style="display:inline" name="formular1" method="post" action="default.cshtml">
                           <select name="phone1" class="dropdown">
                            @foreach(var row in db.Query("SELECT * FROM Handy")){
              <option value="@row.Handyname">@row.Handyname</option>
            }
                                        </select>
        vs.
                           <select name="phone2" class="dropdown">
                                            @foreach(var row in db.Query("SELECT * FROM Handy")){
              <option value="@row.Handyname">@row.Handyname</option>
            }
                                        </select>
        <input type="submit"/ value="Compare">
        </form>
        @{
            var phoneOne = "";
            var phoneTwo = "";
                    if(IsPost){

                        // request input of the select forms
                        phoneOne = Request["phone1"];
                        phoneTwo = Request["phone2"];  
                    }
                }                                     
    </div>

    <div class="content">
        <div class="start"> 
            <p><h2>@phoneOne</h2></p>
            <ul>
            @{
                if(IsPost){
                    foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@phoneOne")){
              <li>processor: @row.Prozessor GHz</li>
                <li>memory: @row.RAM MB Ram</li>
                <li>weight: @row.Gewicht g</li>
                <li>display: @row.Display ''</li>
                <li>OS: @row.OS</li>
            }
                }
            }


            </ul>
        </div>

查询时出错:WHERE Handyname = @ phoneOne ...将其全部删除都可以。我做错了什么?

感谢:)

3 个答案:

答案 0 :(得分:1)

不太确定,但我认为你需要更换它:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@phoneOne"))

有了这个:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname= " + phoneOne))

答案 1 :(得分:1)

如上所述here,请尝试以下操作:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@@phoneOne")){

答案 2 :(得分:0)

db是否是对Razor中数据库组件的引用?如果是这样,它使用@ 0,@ 1,(索引)而不是命名参数。

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@0"))

并通过该方法中的参数集合传递值。