如何创建不接受某个字符的搜索

时间:2018-03-06 23:01:23

标签: c# search

我正在尝试创建一个搜索,当用户搜索带有冒号的内容时,将返回错误。这就是我到目前为止对代码的任何改进都会很棒!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Assignment1
{
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SUBMIT_Click(object sender, EventArgs e)
    { 


        {
            string sConnection = "Data Source=labsql;Integrated Security=True;database=master";
            SqlConnection dbConn;
            dbConn = new SqlConnection(sConnection);
            dbConn.Open();

            string sql = "SELECT * FROM you_found_the_easter_egg WHERE Message LIKE '%" + DataInputBox.Text + "%' OR IsThisFunnyToJeff LIKE '%" + DataInputBox.Text + "%' OR George LIKE '%" + DataInputBox.Text + "%';";
            SqlCommand dbCmd = new SqlCommand(sql);
            dbCmd.Connection = dbConn;

            SqlDataReader reader = dbCmd.ExecuteReader();

            while (reader.Read())

            reader.Close();
            dbCmd.ExecuteNonQuery();
            dbConn.Close();
        }

        }

    }
}

另外,在呈现结果方面我应该使用什么?列表框是否是呈现结果的最简单方法?

1 个答案:

答案 0 :(得分:0)

尝试使用有效的chars属性在文本框中指定要接受的字符

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/ajax-control-toolkit/filteredtextbox/allowing-only-certain-characters-in-a-text-box-cs

public interface IUser
{
    [JsonProperty("id")]
    string DocumentId { get; set; }

    [JsonProperty("created")]
    DateTimeOffset Created { get; set; }

    [JsonProperty("modified")]
    DateTimeOffset Modified { get; set; }

    [JsonProperty("email")]
    string Email { get; set; }

    ...
}