CSHTML - SQL QUERY....WHERE NAME=@NameOne
I'm using 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>
Getting an error with the query: WHERE Handyname=@phoneOne ...leaving it out all works fine. What am I doing wrong?
Thanks:)!
Not very sure, but I think you need to replace this:
foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@phoneOne"))
With this:
foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname= " + phoneOne))
As described here, try the following:
foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@@phoneOne")){
Is db a reference to the database component in Razor? If so, it uses @0, @1, (indexes) not named parameters instead.
foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@0"))
And pass in the value through the collection of parameters in that method too.
精彩评论