C# Code - How To Filter Data In MySQL And Show It On DataGridView Using C#
__________________________________________________________________________
In This C# Tutorial We Will Learn How To Find Data In MySQL Database With A
Specific Value From TextBox And Display The Result In DataGridView Using CSharp
Programming Language .
In This C# Tutorial We Will Learn How To Find Data In MySQL Database With A
Specific Value From TextBox And Display The Result In DataGridView Using CSharp
Programming Language .
Source Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Csharp_And_MySQL
{
public partial class Csharp_MySQL_Datagridview_Search : Form
{
MySqlConnection connection = new MySqlConnection("datasource = localhost;port = 3306; Initial Catalog = 'test_db'; username = root; password=");
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable table;
public Csharp_MySQL_Datagridview_Search()
{
InitializeComponent();
}
private void Csharp_MySQL_Datagridview_Search_Load(object sender, EventArgs e)
{
searchData("");
}
public void searchData(string valueToSearch)
{
string query = "SELECT * FROM users WHERE CONCAT(`id`, `fname`, `lname`, `age`) like '%"+valueToSearch+"%'";
command = new MySqlCommand(query, connection);
adapter = new MySqlDataAdapter(command);
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
private void BTN_SEARCH_Click(object sender, EventArgs e)
{
string valueToSearch = textBoxValueToSearch.Text.ToString();
searchData(valueToSearch);
}
}
}
=> OUTPUT:
Download Projects Source Code
3 comments
commentswhat is CONCAT?
ReplyWhat is wrong with this SQL statement: string query = "SELECT * FROM knjigarna WHERE CONCAT(`ID`, `Naslov`, `Opis`, `Avtor`,`Oznaka`,`Izdajatelj`,`Leto`) like '%" + valueToSearch + "%'";
ReplyЕсли в поиске вводишь ' то все рушится
ReplyMySql.Data.MySqlClient.MySqlException: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''' at line 1"