【问题标题】:C# - Sql Query If Where Clause is there then working if not there no resultC# - Sql Query If Where Clause is there then working if not there no result
【发布时间】:2014-10-30 11:52:03
【问题描述】:

请解决我的问题

我有以下代码

jQuery.ajax({                
                type: 'POST',
                url: "Default.aspx/populate_From",
                data: JSON.stringify({ term: aterm }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data.d);

                    //jQuery(containerFrom).html(data.d);
                    //forceWidth(containerFrom, jQuery(containerFrom).width());
                },
                error: function (x, e) {
                    alert(e.Message);
                    alert(jsonResponse.message);
                    jQuery(containerFrom).text("Error, unable to retrieve list.");
                }
            });
[WebMethod]
    public static string populate_From(string term)
    {
        string query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0 AND (route_from LIKE '" + term + "%' OR map_from LIKE '" + term + "%') ORDER BY priority DESC, state_from ASC, map_from ASC";
        if (string.IsNullOrWhiteSpace(term))
        {
            query = "SELECT * FROM route_onlinemaptable";
        }
        SqlCommand cmd = new SqlCommand(query); 
        return GetData(cmd).GetXml();
    }

    public static DataSet GetData(SqlCommand cmd)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["myConnStrng"].ConnectionString;   
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }

Query = SELECT * FROM route_onlinemaptable --> 不显示任何结果
If Query = SELECT * FROM route_onlinemaptable where route_from = 'SOME VALUE' --> 显示结果

我的数据库附加到应用程序和表包含近 500 - 600 行。

请提出任何建议。

【问题讨论】:

    标签: asp.net web-services c#-4.0 c#-3.0


    【解决方案1】:

    请添加一个条件

    检查 term 是否为 null 或为空,然后不要追加 where 条件

    string query = "";
        if(term = "")
        query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0"
        else
        query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0 AND (route_from LIKE '" + term + "%' OR map_from LIKE '" + term + "%') ORDER BY priority DESC, state_from ASC, map_from ASC";
    

    【讨论】:

    • 嗨 Aarif,即使直接通过 query = "SELECT * FROM route_onlinemaptable" 也无法正常工作
    • 嗨 Aarif,它正在执行,但是 Web 方法响应没有到达 ajax 成功函数,它将进入错误函数。
    • 将 dataType: "json" 更改为 dataType: "xml" 并再次检查。
    • 嗨 Aarif,即使我输入 dataType: "xml" 也无法正常工作,如果选择前 10 行,那么它可以正常工作,我没有获得全部数据
    • 因此可能有一些数据会导致错误。尝试选择前 100 名,然后选择 200 名,依此类推以了解此问题。
    猜你喜欢
    • 2022-01-03
    • 2022-12-01
    • 2022-12-27
    • 2021-07-07
    • 2022-12-26
    • 2022-12-02
    • 2020-10-07
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多