2013-06-01 1 views
0

MySQL 데이터베이스에 연결하고 결과를 나열하는 C# 응용 프로그램을 만들고 있습니다. 두 개의 텍스트 상자를 추가하여 두 텍스트 상자의 텍스트 변경 이벤트에 사용자 ID와 사용자 이름/성으로 데이터를 필터링 할 수있게해야합니다. 난 내 프로그램을 실행하고 내가 EvaluationException이 예외를 얻을 그 텍스트 상자 중 하나에 뭔가를 입력 할 때 코드는 그러나이C# MySQL 바인딩 소스가 평가 예외를 throw합니다.

 private void tbSearchStudents_TextChanged(object sender, EventArgs e) 
     { 
      BindingSource binding = new BindingSource(); 
      binding.DataSource = dataGridViewStudents.DataSource; 
      this.tsProgressBar.Visible = true; 
      this.tsProgressBar.Style = ProgressBarStyle.Marquee; 
      binding.Filter = @"students.uid LIKE '%" + this.tbSearchId.Text + @"%' 
           AND CONCAT_WS(' ' , students.name, students.surname) 
           LIKE '%" + this.tbSearchName.Text + "%'"; 
      this.dataGridViewStudents.DataSource = binding; 
      this.tsProgressBar.Visible = false; 
     } 

과 같습니다 : 난 단지 내가 전에 ID 텍스트 상자를 사용
The expression contains undefined function call CONCAT_WS().

이름/성을 추가하고 단지 students.uid LIKE '%" + this.tbSearchId.Text + @"%' 만 사용하면 정상적으로 작동합니다.

무슨 일입니까? LIKE 표현을 허용하지만 CONCAT이나 CONCAT_WS 함수를 인식하지 못합니까?

답변

1

CONCAT_WS는 문자열을 연결하는 MySql 함수이기 때문에 NET Framework에 알려진 함수이며 BindingSource의 Filter 속성에 적용 할 수 없습니다. 이 컨텍스트에서 MySql 데이터베이스 엔진에 필터를 구문 분석하도록 요청하지 않고 NET Framework와 대화 중이므로 규칙을 따라야합니다.

당신은

binding.Filter = @"students.uid LIKE '%" + this.tbSearchId.Text + @"%' 
        AND students.name + ' ' + students.surname) 
        LIKE '%" + this.tbSearchName.Text + "%'"; 

또한, 내부 따옴표에주의를 기울여야합니다 (BindingSource.Filter 속성은 같은 규칙을 따른다)이를 DataColumn 개체의 Expression 속성에 설명 된대로 문자열에 적용되는 + operator를 사용할 필요가 텍스트 상자에 학생의 성이 O'Hara 인 경우 다른 구문 오류가 발생할 수 있습니다. 안전을 위해 다음을 추가하십시오.

... 
AND students.name + ' ' + students.surname) 
LIKE '%" + this.tbSearchName.Text,Replace("'", "''") + "%'"; 
.... 
+0

감사! 'LIKE'는 저를 버렸습니다. 그리고 나는 어떻게 든 MySQL과 comunicating하고 있다고 생각했습니다. – MrPlow

관련 문제