是否可以从sql server中过滤数据

时间:2019-06-07 09:55:31

标签: c# sql-server

我正在尝试为一家银行显示其贷方/借方应用程序,但是当我在文本框中过滤该应用程序时,找不到数据! 希望你能帮助我!

        string filter = "";
        string command = "SELECT * FROM Movimentos WHERE [Tipo de Movimento] IN('Crédito')";

        if (textTipodeMovimento.Text != "")
        {
            filter = filter + "[Tipo de Movimento] LIKE  '%" + textTipodeMovimento.Text + "%'AND";
        }

        if (filter.Length > 0)
        {
            Sqldata.DataSource = SqlDataSource1;
            string FinalFilter = filter.Remove(filter.Length - 3);
            SqlDataSource1.SelectCommand = command + FinalFilter;
            Sqldata.DataBind();
        }
        else
        {
            Sqldata.DataBind();
        }

    }

1 个答案:

答案 0 :(得分:-1)

如果只更正您的代码,它将像:

    string command = "SELECT * FROM Movimentos WHERE [Tipo de Movimento]  = 'Crédito'"; // no need to use IN statement if there is only a check for 1 value

    if (textTipodeMovimento.Text != "")
    {
        command += String.Format(" AND lower([Tipo de Movimento]) LIKE  '%{0}%'", textTipodeMovimento.Text.ToLower());
    }

    Sqldata.DataBind();