2016-10-27 2 views
-1
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; 

     } 
    } 
} 

나는 검색 기능을 수행했으나 이름으로 데이터베이스를 검색 할 수있다. 게시자와 같은 다른 열을 추가하는 방법, 범주, 위치 등과 같은 책을 표시하는 이름도 입력 할 수있다. 이걸 개선하는 데 도움이 될까요?mysql 데이터베이스에서 다중 열을 검색하는 방법은 무엇입니까?

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; 
} 

당신은 필터를 수정해야합니다,보세요 : 감사합니다

답변

1

내가보기 엔 당신이 동적으로 구축 매개 변수화 된 쿼리 O를 사용하는 것이 좋습니다.

클래스에있는 함수를 추가하고 일부 레코드를 필터링 할 때마다 DataGrid의 bindigsource에 할당하십시오. 일부 변수를 사용하여 필터링 할 내용을 함수에 알리거나 필터를 함수 매개 변수로 전달하여 commandText에 추가하십시오.

DataGrid.DataSource = FilterRecords(); 
+0

죄송합니다. 어떻게 추가합니까? C#의 모든 것을 처음 접했습니다. 방금 시작 했어, 보여 줄 수있어? –

+0

메소드가하는 일을 이해하려고보십시오. – McNets

+0

이봐, 내가 MySQL을 깨닫지 못했고, 내 Sql을 변경 .......... 개체 MySql에 의해 ...... – McNets

관련 문제