|  | 
	                
  
    |  |  
    | 
        
        error ในการ search ข้อมูลครับ ไม่ทราบว่าเป็นข้อมูลแบบ ไหน  ต้องโค๊ดดักอย่างไรครับ     |  
    |  |  
 
              
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | SQL ละครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-27 10:44:32 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | งั้นเอาโคดมาดูก่อนละกัน 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-27 11:22:02 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอโทษนะครับเริ่มเหนื่อยแระ คือที่ให้มาแค่นั้นมันไม่ช่วยแก้ปัญหาได้
 
 อยากดู SQL หรือ โคดไรก็ได้ที่เป็นต้นเหตอ่าครับ
 
 รึ dt มันไม่ต้องใช้ SQL รึ โคดรึครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-27 13:42:47 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อันนี้โค๊ด ของการค้นหาครับ Code (C#)
 
  private void search()
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(
            "select (materials.id),(materials.private_code),(materials.mat_code),(materials.serial),(materials.name),(materials.unit_name),(materials.description),(materials.remark),isnull(mat_onhands.quantity,0) as quantity FROM materials LEFT JOIN mat_onhands ON materials.id=mat_onhands.material_id where (materials.mat_group_id like'" + P_group.SelectedValue + "%' and materials.mat_category_id like'" + P_category.SelectedValue + "%' and(materials.name like'" + text_search.Text + "%' or materials.serial like'" + text_search.Text + "%') )"
           , conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow item in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = item["private_code"].ToString();
                dataGridView1.Rows[n].Cells[1].Value = item["mat_code"].ToString();
                dataGridView1.Rows[n].Cells[2].Value = item["serial"].ToString();
                dataGridView1.Rows[n].Cells[3].Value = item["name"].ToString();
                dataGridView1.Rows[n].Cells[4].Value = item["unit_name"].ToString();
                dataGridView1.Rows[n].Cells[5].Value = item["description"].ToString();
                dataGridView1.Rows[n].Cells[6].Value = item["remark"].ToString();
                dataGridView1.Rows[n].Cells[7].Value = item["quantity"].ToString();        
            }
            conn.Close();
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("No Material Data ", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            text_search.Clear();
            text_search.Focus();
        }
 อันนี้ UI ครับ
 
  
 |  
              | 
 ประวัติการแก้ไข
 2015-10-27 17:13:02
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-27 17:10:38 | By :
                            phuriwat |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Error ตอนที่ text_search.Text = ' ใช่ไม๊ครับ สามารถดักจับได้ก็ประมาณ
 
 Code (C#)
 
 text_search.Text = (text_search.Text == "'")? "":text_search.Text;
 
 รึไม่ก็
 
 Code (C#)
 
 MessageBox.Show("select (materials.id),(materials.private_code),(materials.mat_code),(materials.serial),(materials.name),(materials.unit_name),(materials.description),(materials.remark),isnull(mat_onhands.quantity,0) as quantity FROM materials LEFT JOIN mat_onhands ON materials.id=mat_onhands.material_id where (materials.mat_group_id like'" + P_group.SelectedValue + "%' and materials.mat_category_id like'" + P_category.SelectedValue + "%' and(materials.name like'" + text_search.Text + "%' or materials.serial like'" + text_search.Text + "%') )")
 เพื่อดูว่าได้ตามที่ต้องการรึป่าวครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-27 18:11:33 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | MS SQL ถ้าจะใช้ ' ให้เปลี่ยนไปเป็น '' ครับ
 
 ตัวอย่างครับ
 Code (C#)
 
 private string S(string txt)
        {
                return "\'" + txt.Replace("\'", "\'\'") + "\'";
        }
private void search()
{
       string Sqlcmd;
       SqlCmd=" select * from TableName where field1=" + S(txtsearch.text);
}
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 08:57:17 | By :
                            fonfire |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แล้ว ถ้าทำไห้ textbox ไม่พิม ตัว '  นี้ ตัว เดียวต้องทำไงครับ เหมือนแบบ ไม่ไห้พิมตัวอักษรอ่าครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 10:52:37 | By :
                            phuriwat |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ความจริงมันผิดหลักอ่าครับ คือเราต้องการค้นหาคำว่า
 
 'g'
 
 พอลงใน SQL syntex
 จะกลายเป็น
 
 like''g'%'
 
 ซึ่งความจริง user คงไม่ค่อยใช้งานแบบนี้อ่าครับ
 เท่ากับว่าเราจะต้องเพิ่ม skill ให้ user เป็นการ make job ให้ user นะครับ
 ส่วนใหญ่ก็แค่ค้น g อย่างเดียว
 
 
 ลองแจ้งวัตถุประสงค์การค้นหามาดูจะได้ช่วยกันออกแบบได้ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 11:07:43 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คือที่เอา S(text_search.Text) ไปต่อแบบนั้นทำให้คำสั่ง SQL มันผิดอ่ะครับ ถ้าไม่อยากให้มี ' อาจจะไม่ต้อง lock ก็ได้ครับ
 ใช้คำสั่ง replace แทนก็ได้
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 11:18:05 | By :
                            fonfire |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | // เอา ' ออกจาก textbox text_search.Text=text_search.Text.Replace("\'", "") ;
 
 string sq1 =
 "select (materials.id),(materials.private_code),(materials.mat_code),(materials.serial),(materials.name),(materials.unit_name),(materials.description),(materials.remark),isnull(mat_onhands.quantity,0) as quantity FROM materials LEFT JOIN mat_onhands ON materials.id=mat_onhands.material_id where (materials.mat_group_id like'" + P_group.SelectedValue + "%' and materials.mat_category_id like'" + P_category.SelectedValue + "%' and (materials.name like'" + text_search.Text + "%' or materials.serial like'" + text_search.Text + "%') )";
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 11:25:12 | By :
                            fonfire |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้แล้วครับ ขอบคุณทุกคนมากครับ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 11:38:00 | By :
                            phuriwat |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | สมมุติว่าผมเป็น Users ก็จริง แต่ความต้องการมันแปลกปะลาตมาก มันใหญ่ใหญ่พอฯกับเจ้าของบริษัท เช่นต้องการค้นคำว่า
 
 can't
 ห'อวบ
 
 ปัญหาของคุณมันก็ยังไม่จบ (ไม่ครอบคลุมทุกฯกรณี) เป็นต้น
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 12:14:30 | By :
                            หน้าฮี |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ความจริงดักจับคำต้องห้ามไว้เลยก็ได้นิครับ 
 Code (C#)
 
 private void text_search_TextChanged(object sender, EventArgs e)
        {
            string[] cri = new string[] { "'", "เ", "ห", "ี", "้", "ย" };
            foreach (string s in cri)
            {
                text_search.Text = (text_search.Text.Substring(0, 1) == s) ? text_search.Text.Substring(1, text_search.Text.Length - 1) : text_search.Text;
                text_search.Text = (text_search.Text.Substring(text_search.Text.Length - 1, 1) == s) ? text_search.Text.Substring(0, text_search.Text.Length - 1) : text_search.Text;
            }
        }
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 13:55:58 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | MS SQL ถ้าจะใช้ ' ให้เปลี่ยนไปเป็น '' ครับ
 
 ตัวอย่าง
 select * from table1 where field1= 'Can''t'
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-10-28 15:05:28 | By :
                            fonfire |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 |  |