如何在c#中使用单选按钮进行搜索

发布于 2025-01-11 01:56:20 字数 1779 浏览 5 评论 0原文

表格表单

我尝试使用 RadioButton 过滤此表格DateDePublication选中,且搜索文本的值等于(例如 2000 表)应返回所有具有DateDePublication 等于 2000

这是搜索按钮代码:

private void RechBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = repository.GetAllLivres(rechtext.Text);

        }

以及返回所有书籍的搜索方法代码:

public List<Livre> FilterDateDePublication(string date)
    {
        using (var connection = factory.CreateConnection())
        {
            var livres = new List<Livre>();
            connection.ConnectionString = connectionString;
            connection.Open();
            var command = factory.CreateCommand();
            command.Connection = connection;
            command.CommandText = "select * from livre where date_publication like '%" + date + "%'";
            using (DbDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Livre l = new Livre();
                    l.Isbn = reader["isbn"].ToString();
                    l.Titre = reader["titre"].ToString();
                    l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                    l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                    l.Couverture = reader["couverture"].ToString();
                    l.Prix = Double.Parse(reader["nombre_page"].ToString());
                    l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
                }
            }
            return livres;

        }

Table Form

I'm trying to filter this table with RadioButton when DateDePublication is Checked and the value of the search text equals for example 2000 table should return all books who have DateDePublication equals to 2000

this is Search Button code :

private void RechBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = repository.GetAllLivres(rechtext.Text);

        }

and search method code to return all books :

public List<Livre> FilterDateDePublication(string date)
    {
        using (var connection = factory.CreateConnection())
        {
            var livres = new List<Livre>();
            connection.ConnectionString = connectionString;
            connection.Open();
            var command = factory.CreateCommand();
            command.Connection = connection;
            command.CommandText = "select * from livre where date_publication like '%" + date + "%'";
            using (DbDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Livre l = new Livre();
                    l.Isbn = reader["isbn"].ToString();
                    l.Titre = reader["titre"].ToString();
                    l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                    l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                    l.Couverture = reader["couverture"].ToString();
                    l.Prix = Double.Parse(reader["nombre_page"].ToString());
                    l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
                }
            }
            return livres;

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2025-01-18 01:56:20

您的函数 GetAllLivres 只是查找标题中带有特定字符串的书籍。

command.CommandText = "select * from livre where titre like '%" + mc + "%'";

您应该将其更改为在发布日期进行搜索。 Can 说应该是什么样子,因为我们不知道你的数据库方案。

顺便说一句,不要像那样构建 SQL,它是一个巨大的安全漏洞。使用参数化查询

your function GetAllLivres just looks for books with a specifc string in their title.

command.CommandText = "select * from livre where titre like '%" + mc + "%'";

You should change that to search on the pub date. Can says how that should look because we dont know your database scheme.

By the way , do not build SQL like that, its a huge security hole. Use parametrized queries

孤独难免 2025-01-18 01:56:20

我知道我有 4 个单选按钮,当选中其中一个时,搜索功能应通过每个单选按钮进行查找。

好吧,再次没有看到用户界面很难说,但我猜你有类似的东西,

   |-search by: --------| <<< group box
   | ( ) Author         |  << radio buttons
   | ( ) title          |
   | (*) Year           |
   ----------------------
   Search for :_________________: <<== text box
   [Start Search]                 <<== button

我希望如此。

所以这样做

 if(radioYear.Checked){
        FilterDateDePublication(searchText.Text);
 }
 else if(radioAuthor.Checked)
    .......

请注意,用户输入的日期可能与数据库中的日期格式不匹配,在这种情况下,您需要进行一些处理,

但是我会重构您的代码,您肯定已经注意到,您的 FilterDateDePublication 的 90% 是和标题一一样。

你应该这样做

public List<Livre> ReadBooks(string query)
{
    using (var connection = factory.CreateConnection())
    {
        var livres = new List<Livre>();
        connection.ConnectionString = connectionString;
        connection.Open();
        var command = factory.CreateCommand();
        command.Connection = connection;
        command.CommandText = query;
        using (DbDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Livre l = new Livre();
                l.Isbn = reader["isbn"].ToString();
                l.Titre = reader["titre"].ToString();
                l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                l.Couverture = reader["couverture"].ToString();
                l.Prix = Double.Parse(reader["nombre_page"].ToString());
                l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
            }
        }
        return livres;

    }

,然后

   List<Livre> GetByDate(string date){
        return GetBooks("select * from livre where date_publication like '%" + date + "%'";

    }

更好的是使用 sql 参数。我稍后会更新这个答案

I know i have 4 radio buttons when one of them is checked Search function should look up by each of radio button.

Well again not seeing the UI its hard to say but my guess is you have something like this

   |-search by: --------| <<< group box
   | ( ) Author         |  << radio buttons
   | ( ) title          |
   | (*) Year           |
   ----------------------
   Search for :_________________: <<== text box
   [Start Search]                 <<== button

I hope so.

So do this

 if(radioYear.Checked){
        FilterDateDePublication(searchText.Text);
 }
 else if(radioAuthor.Checked)
    .......

Note that the date the user enters might not match the format of the date in the database, in that case you need to do some massaging

I would however refactor your code, you have surely noticed that 90% of your FilterDateDePublication is the same as the title one.

You should do

public List<Livre> ReadBooks(string query)
{
    using (var connection = factory.CreateConnection())
    {
        var livres = new List<Livre>();
        connection.ConnectionString = connectionString;
        connection.Open();
        var command = factory.CreateCommand();
        command.Connection = connection;
        command.CommandText = query;
        using (DbDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Livre l = new Livre();
                l.Isbn = reader["isbn"].ToString();
                l.Titre = reader["titre"].ToString();
                l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                l.Couverture = reader["couverture"].ToString();
                l.Prix = Double.Parse(reader["nombre_page"].ToString());
                l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
            }
        }
        return livres;

    }

and then have

   List<Livre> GetByDate(string date){
        return GetBooks("select * from livre where date_publication like '%" + date + "%'";

    }

even better would be to use sql parameters. I will update this answer later

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文