Razor动态SQL查询

时间:2014-07-22 20:10:03

标签: c# asp.net sql razor

我有以下C#代码。

var var_db = Database.Open("Intranet") ;
var var_selectCommand = "SELECT Ticket_ID AS [Ticket], Logged_Date AS [Created], Ticket_Type AS [Ticket Type], Department, Priority, Forename, Surname, Ticket_Status AS [Status], Completed_Date AS [Completed], User_Assigned AS [User], Ticket_Subject AS [Subject] FROM Ticket_Data WHERE Ticket_Status = 'Open' ORDER BY Logged_Date DESC";
var var_searchTerm = "";

if(!Request.QueryString["Priority"].IsEmpty() ) {
    var_selectCommand = "SELECT Ticket_ID AS [Ticket], Logged_Date AS [Created], Ticket_Type AS [Ticket Type], Department, Priority, Forename, Surname, Ticket_Status AS [Status], Completed_Date AS [Completed], User_Assigned AS [User], Ticket_Subject AS [Subject] FROM Ticket_Data WHERE Priority = @0 ORDER BY Logged_Date DESC";
    var_searchTerm = Request.QueryString["Priority"];
}

if(!Request.QueryString["Department"].IsEmpty() ) {
    var_selectCommand = "SELECT Ticket_ID AS [Ticket], Logged_Date AS [Created], Ticket_Type AS [Ticket Type], Department, Priority, Forename, Surname, Ticket_Status AS [Status], Completed_Date AS [Completed], User_Assigned AS [User], Ticket_Subject AS [Subject] FROM Ticket_Data WHERE Department = @0 ORDER BY Logged_Date DESC";
    var_searchTerm = Request.QueryString["Department"];
}

我有以下标记。

            <select class="field" id="priority" name="Priority" >
                <option value="">Please choose...</option>
                <option value="Low">Low</option>
                <option value="Medium">Medium</option>
                <option value="High">High</option>
            </select>

            <select class="field" id="department" name="Department" >
                <option value="">Please choose...</option>
                <option value="Department 1">Department 1</option>
                <option value="Department 2">Department 2</option>
                <option value="Department 3">Department 3</option>
                <option value="Department 4">Department 4</option>
                <option value="Department 5">Department 5</option>
                <option value="Department 6">Department 6</option>
            </select>

如果我想通过&#39;优先级&#39;进行搜索或者&#39;部门&#39;只有这样才行,但我想要做的是应用多个搜索条件并让它动态更改SQL查询,这样我就可以只返回特定优先级的结果和特定部门的结果。

有人能告诉我实现这个目标的最佳途径吗?

1 个答案:

答案 0 :(得分:0)

您可以使用一些字符串连接来构建查询。这应该可以帮到你。

var selectStatement = "SELECT Ticket_ID AS [Ticket], Logged_Date AS [Created], Ticket_Type AS [Ticket Type], Department, Priority, Forename, Surname, Ticket_Status AS [Status], Completed_Date AS [Completed], User_Assigned AS [User], Ticket_Subject AS [Subject] FROM Ticket_Data ";
List<string> whereConditions = new List<string>();

if(!Request.QueryString["Priority"].IsEmpty() ) {
    whereConditions.Add(" Priority = yourPriorityParameterHere ");
    //.....
}

if(!Request.QueryString["Department"].IsEmpty() ) {
    whereConditions.Add(" Department = yourDepartmentParameterHere ");
    //.....
}
var commandText = selectClause + " WHERE "+ string.Join(" AND ", whereConditions) + " ORDER BY LoggedDate DESC";
//......
//Fill in parameters, execute sql, live happy