SqlServer參數(shù)化查詢之where in和like實(shí)現(xiàn)之xml和D建站
導(dǎo)讀:1建站知識(shí)在上一篇Sql Server參數(shù)化查詢之where in和like實(shí)現(xiàn)詳解中介紹了在Sql Server使用參數(shù)化查詢where in的幾種實(shí)現(xiàn)方案,遺漏了seo網(wǎng)站優(yōu)化seo網(wǎng)站關(guān)鍵詞優(yōu)化。
方案5 使用xml參數(shù) 對(duì)sql server xml類型參數(shù)不熟悉的童鞋需要先了解下XQuery概念,這里簡(jiǎn)單提下XQuery 是用來從 XML 文檔查找和提取元素及屬性的語言,簡(jiǎn)單說就是用于查詢xml的語言說到這就會(huì)牽著到XPath,其實(shí)XPath是XQuery的一個(gè)子集,XQuery 1.0 和 XPath 2.0 共享相同的數(shù)據(jù)模型,并支持相同的函數(shù)和運(yùn)算符,XPath的方法均適用于XQuery,假如您已經(jīng)學(xué)習(xí)了 XPath,那么學(xué)習(xí) XQuery 也不會(huì)有問題。詳見 XQuery概念了解后需要進(jìn)一步了解下Sql Server對(duì)xml的支持函數(shù),主要為query()、nodes()、exist()、value()、modify() ,詳見
使用xml方式實(shí)現(xiàn)where in時(shí)有兩種實(shí)現(xiàn)方式,使用value和exist,在這里推薦使用exist方法,msdn是這樣描述的:D.使用 exist() 方法而不使用 value() 方法 由于性能原因,不在謂詞中使用 value() 方法與關(guān)系值進(jìn)行比較,而改用具有 sql:column() 的 exist()。 使用xml的value方法實(shí)現(xiàn)(不推薦)
復(fù)制代碼 代碼如下:
DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString)) { string xml = @" <root> <UserID>1</UserID> <UserID>2</UserID> <UserID>5</UserID> </root>"; SqlCommand comm = conn.CreateCommand(); //不推薦使用value方法實(shí)現(xiàn),性能相對(duì)exist要低 comm.CommandText = @"select * from Users where exists ( select 1 from @xml.nodes('/root/UserID') as T(c) where T.c.value('text()[1]','int')= Users.UserID )"; //也可以這樣寫,結(jié)果是一樣的 //comm.CommandText = @"select * from Users // where UserID in // ( // select T.c.value('text()[1]','int') from @xml.nodes('/root/UserID') as T(c) // ) comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml }); using (SqlDataAdapter adapter = new SqlDataAdapter(comm)) { adapter.SelectCommand = comm; adapter.Fill(dt); } }
使用xml的exist方法實(shí)現(xiàn)(推薦)復(fù)制代碼 代碼如下:
DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString)) { string xml = @" <root> <UserID>1</UserID> <UserID>2</UserID> <UserID>5</UserID> </root>"; SqlCommand comm = conn.CreateCommand(); //使用xml的exist方法實(shí)現(xiàn)這樣能夠獲得較高的性能 comm.CommandText = @"select * from Users where @xml.exist('/root/UserID[text()=sql:column(""UserID"")]')=1"; comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml }); using (SqlDataAdapter adapter = new SqlDataAdapter(comm)) { adapter.SelectCommand = comm; adapter.Fill(dt); } }
列舉下不同xml結(jié)構(gòu)的查詢方法示例,在實(shí)際使用中經(jīng)常因?yàn)椴煌膞ml結(jié)構(gòu)經(jīng)常傷透了腦筋聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-21 ,文章SqlServer參數(shù)化查詢之where in和like實(shí)現(xiàn)之xml和D建站主要講述參數(shù),標(biāo)簽,SqlServer參數(shù)化查詢之where in和網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請(qǐng)保留鏈接: http://www.bifwcx.com/article/web_3982.html
為你推薦與SqlServer參數(shù)化查詢之where in和like實(shí)現(xiàn)之xml和D建站相關(guān)的文章
-
通王TWCMS 2.0.3網(wǎng)站模板程序下載
(126)人喜歡 2024-01-15 -
Windows官方原版在哪里下載
(175)人喜歡 2024-01-15 -
WordPress網(wǎng)站模板發(fā)帖標(biāo)題顏色設(shè)置
(131)人喜歡 2024-01-07 -
修改discuz論壇帖子標(biāo)題80字符的長(zhǎng)度限制
(249)人喜歡 2024-01-07 -
wordpress程序調(diào)用不帶超鏈接的Tag標(biāo)簽
(234)人喜歡 2024-01-05 -
網(wǎng)站在不同時(shí)期需調(diào)整內(nèi)容更新的方向
(112)人喜歡 2023-08-12