Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在mysql数据库中搜索多列?_C#_Mysql_Search - Fatal编程技术网

C# 如何在mysql数据库中搜索多列?

C# 如何在mysql数据库中搜索多列?,c#,mysql,search,C#,Mysql,Search,我已经做了搜索功能,但它只是可以搜索我的数据库的名称,如何添加另一列,如出版商,我键入的名称,它还将显示图书相同的类别,位置等。有人可以帮助我改善这一点吗?谢谢我强烈建议您使用参数化查询动态构建它: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.

我已经做了搜索功能,但它只是可以搜索我的数据库的名称,如何添加另一列,如出版商,我键入的名称,它还将显示图书相同的类别,位置等。有人可以帮助我改善这一点吗?谢谢

我强烈建议您使用参数化查询动态构建它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ICT_Assigment_3
{
    public partial class search : Form
    {
        public search()
        {
            InitializeComponent();
        }

        DataTable dbdataset;

        private void button1_Click(object sender, EventArgs e)
        {
            string constring = "datasource=localhost;port=3306;username=root;password=password";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(" select BookName,Publisher,Category,Edition,Year,Location from library.add_update ;", conDataBase);
            try
            {
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                sda.Update(dbdataset);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView DV = new DataView(dbdataset);
            DV.RowFilter = string.Format("BookName LIKE '%{0}%'", search_box.Text);
            dataGridView1.DataSource = DV;

        }
    }
}
看一看,您必须修改过滤器

在类中添加这样的函数,并在每次需要筛选某些记录时将其分配给datagrid的bindigsource。使用一些变量告诉函数要过滤的内容,或者将过滤器作为函数参数传递并将其添加到commandText中

private DataTable FilterRecords()
{
    bool where_set = false;

    DataTable table = new DataTable();

    string constring = "datasource=localhost;port=3306;username=root;password=password";

    string commandText = "select BookName, Publisher, Category, Edition, Year,"
                         + "Location from library.add_update "
                         + "where "

    // build your own filters
    //
    if (filter_by_name)
    {
        commandText += "name like '%" + varName + "%'";
        where_set = true;
    }

    if (filter_by_publisher)
    {
        if (where_set) commandText += " and ";
        commandText += "name like '%" + varName + "%'";
        where_set = true;
    }

    using (MySqlConnection connection = new MySqlConnection(constring))
    {
        MySqlDataAdapter adp = new MySqlDataAdapter();
        adp.SelectCommand = new MySqlCommand(commandText, connection);
        adp.Fill(table);
    }

    return table;
}

抱歉,如何将此添加到?我对C#的一切都不熟悉。我刚开始,你能给我看看吗?看一看,试着理解这个方法的作用。嘿,我没有意识到它是一个mysql,改变我的Sql。。。。。。。。。。MySql的对象。。。。。。
DataGrid.DataSource = FilterRecords();