2014-02-10 1 views
0

나는 mcID를 사용하여 서로 관계가있는 약속 테이블과 의료 센터 인 두 개의 테이블을 가지고 있습니다. 지금 내 약속 양식, 나는 gridview mcID 대신 medicalcentre 테이블에 mcCentre 표시하려면 외부 조인을 사용합니다. 내 양식에 보면 모든 의료 센터 (mcCentre)가 gridview에 표시됩니다. 그러나 황 황산 가족 클리닉의 텍스트 상자에있는 텍스트와 일치시키려는 경우에만 황 및 리앙 패밀리 클리닉 기록을 표시하고 싶습니다. 즉, 병원 텍스트가 텍스트 상자에 있다면 병원 기록이 그리드 뷰에 표시되기를 바랍니다. 텍스트 상자 이름은 txtCentre입니다.텍스트 상자의 텍스트를 일치시켜 레코드를 표시하는 방법은 무엇입니까?

enter image description here

enter image description here

private void LoadAppointmentRecords() 
{ 

    //retrieve connection information info from App.config 
    string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString; 
    //STEP 1: Create connection 
    SqlConnection myConnect = new SqlConnection(strConnectionString); 
    //STEP 2: Create command 
    //string strCommandText = "SELECT appointmentID, convert(varchar, aDate, 103) AS aDate, aTime, aStatus, aContact, aHeight, aWeight, patientID, mcID, nurseID FROM APPOINTMENT"; 

    string strCommandText = "SELECT appointmentID, convert(varchar, aDate, 103) AS aDate, aTime, aStatus, aContact, aHeight, aWeight, pat.pFirstName, pat.pLastName, cen.mcCentre, nur.nUsername FROM APPOINTMENT AS app"; 
    strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid"; 
    strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as cen on app.mcid = cen.mcid"; 
    strCommandText += " LEFT OUTER JOIN NURSE as nur on app.nurseid = nur.nurseid"; 
    //strCommandText += " LEFT OUTER JOIN NURSE as nur on app.nurseid = nur.nurseid"; 

    AppointmentAdapter = new SqlDataAdapter(strCommandText, myConnect); 

    //command builder generates Select, update, delete and insert SQL 
    // statements for MedicalCentreAdapter 
    //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(AppointmentAdapter); 
    // Empty Employee Table first 
    Appointment.Clear(); 
    // Fill Employee Table with data retrieved by data adapter 
    // using SELECT statement 
    AppointmentAdapter.Fill(Appointment); 

    // if there are records, bind to Grid view & display 
    if (Appointment.Rows.Count > 0) 
     grdApp.DataSource = Appointment; 
} 

답변

1

... 이것은 텍스트가 완전히 몇 일치, 아직 입력하지 않아도 때 txtCentre.Text에 무엇이든 일치하여 문자열이 코드를 추가합니다 문자 또는 단어가 할 것입니다.

strCommandText += " WHERE mcCentre like '%" + txtCentre.Text.Replace("'", "''").Trim() + "%'"; 

strCommandText += " WHERE mcCentre like '" + txtCentre.Text.Replace("'", "''").Trim() + "'"; 

이 .Replace 추가 ... 정확히 txtCentre.Text에 무엇이든 일치합니다 (" '", "' '") .Trim() 당신의 텍스트에 당신을 도움이 될 것입니다 매개 변수를 사용하지 않고 SQL 삽입을 피하십시오. 그러나 매개 변수를 사용하려면 Jon Barker의 방법을 따라갈 수 있습니다.

0

이 방법을 사용하면 SQL injection 공격에 노출됩니다. 엔티티 프레임 워크와 같은 ORM을 사용하는 것이 좋습니다. chris_techno25가 게시 된 상태에서 직접 SQL로 작업하려면 여전히 사용자로부터 문자열을 직접 포함시키지 말고 매개 변수를 사용하십시오.

http://www.dotnetperls.com/sqlparameter

관련 문제