轉 C# , ASP.Net 中 關於 like in 實現參數化查詢的問題

來源:互聯網
上載者:User
 C# , ASP.Net 中 關於 like in 實現參數化查詢的問題。2008-09-18 18:17對於 普通的 select等sql語句, 正常的參數化 語句 格式:

          select * from profile where EmployeeID= @EmployeeID

   for example:

     string loginString = "select * from profile where EmployeeID= @EmployeeID";

but please attention to the like sql   sentence:      

       select * from profile where EmployeeID Like ‘%’ + @EmployeeID + ‘%’;   

   The accurate search format is :

       Select * from profile where EmployeeID like +@EmployeeID ;

So the

        String   = "SELECT   * from   Box   WHERE   BoxID   like   '%'   +   @subString   +   '%'"

對本文提供了有價值的文章有:

c# sql like 參數

2008-08-08 10:09

參數化的意義在於把對應的值從參數中提供,對於like語句,like後面的值則包括了單引號中的所有部分,包括百分比符號(%),因此在參數化like對應的值時,應該把百分比符號移到參數值中提供,像這樣:

Cmd.Parameters["@KeyWord"].Value = "%" + StrKeyWord + "%";

可別奢想在sql語句中像這樣的樣子:

Select * From [TableName] Where [Column1] like '%@KeyWord%'

不會報錯,不過你不可能查詢到想要的結果.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.