|
SQL Server 2008 การ SELECT แบบ LIKE % % ใน Query Builder |
SQL Server 2008 การ SELECT แบบ LIKE % % ใน Query Builder พอดีวันนี้นั่งเขียนโปแรกรม ฐานข้อมูลลูกค้าอยู่ มีจุดที่ต้องการค้นหา โดยใช้ like % % ซึ่งปกติ ผมจะมักใช้งาน query builder มากว่า มานั่งเขียนโค้ดเองอยู่แล้ว
เอาละครับ เรามาดู like % % ตามบทความนี้กันก่อนครับ
https://www.thaicreate.com/tutorial/sql-like.html
https://www.w3schools.com/sql/sql_like.asp
เท่าที่เห็น select ไม่ได้ซับซ้อนอะไรมากมาย ครับ
รูปแบบมีแค่
Code (C#)
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern
ผมจะลองใช้กับตาราง Postcode ดูนะครับ

ง่ายใช่ไม๊ครับ
ทีนี้ผมจะลองใช้ like ดูนะครับ
การใช้ like สามารถ พิมพ์ ข้อความที่เราต้องการลงใน ช่อง filter ได้เลย แบบนี้

พอเรา Excuted Query จะเห็นว่า ตรงช่องที่เรา พิมพ์ไว้ จะมี ' คล่อมไว้ แบบนี้ ครับ

ทีนี้ เราจะใช้ like % ดู
โดยพิมพ์ like %ข้อความที่ต้องการค้น หรือ like ข้อความที่ต้องการค้น% หรือ like %ข้อความที่ต้องการค้น%
แบบนี้ ครับ

นอกจากนี้แล้ว เรายังสารมารถ ค้นหาแบบใส่ paramter ได้ด้วยนะครับ
อ่านเพิ่มเติมได้จาก
https://www.thaicreate.com/community/csharp-add-query-parameter-in-wizard-ban-ban/view.html
การใส่ paramter จะต้องมี @ นำหน้า คำค้น ครับ
เช่น ผมจะให้ user ค้น จาก รหัสไปรษณี
ผมก็จะเขียนดังนี้ครับ

รูปแบบจะเป็น แบบนี้ครับ
PostCode LIKE '%' + @Cri + '%'
Excuted Query จะเห็นว่ามี หน้า dialog ขึ้นมาเพื่อให้เรา กรอกข้อมูลที่ต้องการค้นหาลงไป

พอเราค้นหาจะได้แบบนี้ครับ

ไม่คิดว่าจะรหัสไปรษณี %123% เลยนะเนี๊ยะ 5555
ทีเด็ดมันอยู่ตรงนี้ครับ
ผมจะให้ user ค้น จาก รหัสไปรษณี หรือ จังหวัด หรือ อำเภอ หรือ ตู้ รหัสไปรษณี
เราก็ ก๊อบ โค้ดเดิมเรา ไปใส่ในช่อง Or ของ column ที่จะค้นต่อไป
ในที่นี้ ผมมี PostCode แล้ว
ผมจะค้นจาก PostOffice/Amphur/Province ก็ก๊อบ LIKE '%' + @Cri + '%' ไปวางดังรูป

Excuted Query จะเห็นว่ามี หน้า dialog ขึ้นมาเพื่อให้เรา กรอกข้อมูลที่ต้องการค้นหาลงไป
ค้นแบบ 1


ค้น 2


ค้น 3


เรามาลองใช้งานกันครับ





แหล่มดีไม๊ละครับ
อันที่ที่จริง ผมเพิ่งรู้วันนี้(2018-08-22) นี่แหละครับ ว่ามันทำแบบนี้ก็ได้ เลย เอามาเขียนให้อ่านกัน
การเพิ่ม parameter like % การค้นหาใน wizard แบบบ้านๆ ก็ขอจบลงเพียงเท่านี้ครับ เนื่องจากใช้ความรู้แบบบ้าน ๆ จึงได้โค้ด และ หลักการแบบบ้านๆ มาเขียนบทความแบบบ้านๆ ให้อ่านกันครับ
Edit 2018-08-22 15:43
สำหรับการค้นหา แบบ เทพๆ ยังมีต่ออีกครับ
หลังจากนั่งศึกษา อีก ครึ่งวัน ทำให้ได้ Stored Procedure ที่ใช้สำหรับการค้นหา ออกมาด้วยครับ
ข้อดีของ Stored Procedure คือ
เมื่อเราสร้างโปรเจคใหม่ หรือ มี หลายๆ โปรเจคที่ต้องใช้ ฐานข้อมูลเดียวกัน
เราจะไม่ต้อง มาคอยสร้าง โค้ดซ้ำ ๆ ในโปรแกรม
สำหรับ Stored Procedure สามารถศึกษาเพิ่มเติมได้ตามนี้ ครับ
https://www.thaicreate.com/tutorial/oracle-stored-procedure.html
ย้ำว่าอ่านแค่ ครึ่งวัน กับความตั้งใจลองผิดลองถูกนิดๆหน่อยๆก็ได้แล้วครับ
ยกตัวอย่าง Stored Procedure ที่ผมเพิ่งสร้างเสร็จนะครับ

Code (C#)
USE [DATALAB]
GO
/****** Object: StoredProcedure [dbo].[PostCodeByLike] Script Date: 08/22/2018 15:48:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[PostCodeByLike]
@Cri varchar(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * from View_Postcode where View_Postcode.PostCode like '%'+@Cri+'%'
or View_Postcode.Post_Amphur like '%'+@Cri+'%'
or View_Postcode.Post_PostOffice like '%'+@Cri+'%'
or View_Postcode.Post_Province like '%'+@Cri+'%'
END
แต่ตอนที่ดึง Stored Procedure ไปลงใน Wisard
จะต้องเลือก Stored Procedure ไม่ใช่ SQL

แล้วก็เลือก Stored Procedure ที่ต้องการได้เลย

ผมเพิ่งหัดทำ จะไม่มี Insert Update Delete ใดๆ นะครับ

รู้สึกจะไม่มี Fill ก็เลยต้อง Add ใหม่(เริ่มจะไม่ยากแล้วครับ หลังจากลองผิดลองถูกมาพักใหญ่)




เรียบร้อยครับ

ลองทดสอบกับ หน้าฟอร์มดู

ในส่วนของโค้ดจะไม่มี Form_load มาให้เหมือนเราลากตัว ปกติมาลงใน ฟอร์ม นะครับ
และส่งผลให้ ไม่
TableAdapter.Fill
มาให้ เวลาเปิดมา จะกลายเป็นหน้าว่างๆ
จึงเป็นเหตุผลว่า ทำไม ผมจึงต้อง Add Fill ขึ้นมา
ก็เพื่อ รองรับ ตอน เปิดหน้า ฟอร์ม ขึ้นมานั่นเองครับ
Code (C#)
public partial class frmPostCode_View : Form
{
public frmPostCode_View()
{
InitializeComponent();
this.postCodeByLikeDataGridView.SetGridDefaultCellStyle();
}
private void fillLikeCriToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.postCodeByLikeTableAdapter.FillLikeCri(this.dATALAB_Postcode.PostCodeByLike, criToolStripTextBox.Text);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void frmPostCode_View_Load(object sender, EventArgs e)
{
this.postCodeByLikeTableAdapter.FillBy(this.dATALAB_Postcode.PostCodeByLike);
}
}
ลองรันดู

ใช้ได้ปกติ ครับ
แบบนี้ เราก็สามารถเอา Stored Procedure ไปรันกับโปรแกรมอื่น ที่ใช้ฐานข้อมูลเดียวกันได้สบายครับ

|
|
|
|
 |
|
|
|
By : |
TOR_CHEMISTRY
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
  |
|
Create Date : |
2018-08-22 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|