SQL语句对象化,先看示例代码.
生活随笔
收集整理的這篇文章主要介紹了
SQL语句对象化,先看示例代码.
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
說明:當(dāng)前只是針對SQLServer數(shù)據(jù),其它的數(shù)據(jù)庫我也沒有用過,不過在基礎(chǔ)上擴展重構(gòu)工作量不是很大.
IN語句
?'SELECT?XSH_HT_合同.合同編號,XSH_HT_合同.合同名稱?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(@p70798e19b6c748519ab37515e1884d8b,@pd0873785429445d8868c51dfd310b31f,@p7d84bb7aa5e340909e02527948ca63c7)?GROUP?BY?XSH_HT_合同.合同編號,XSH_HT_合同.合同名稱?ORDER?BY?XSH_HT_合同.合同編號?ASC
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._合同編號,?en._合同名稱).?_
?????????????????????????????????????????????????????????????????????????From(en).WHERE(en._合同編號.Filter._In("020482",?"020503",?"020539")).?_
?????????????????????????????????????????????????????????????????????????GroupBy(en._合同編號,?en._合同名稱).?_
?????????????????????????????????????????????????????????????????????????OrderBy(en._合同編號.OrderBy(SortDirection.DESC))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
IN語句
???????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pc93cea79f9884d15ae19367bdcab5d0c)
????????'使用IN?語句返回所有[鄧山]的合同.
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) IN語句
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pc93cea79f9884d15ae19367bdcab5d0c)
????????'使用IN?語句返回所有[鄧山]的合同.
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
? '左匹配
?'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@p382468e15c184543b73bbd0434f8440a)?OR?XSH_HT_合同.銷售員?Like?@p1b7b18f217a846e2ae7391320326e857
????????'使用IN?和?Like?語句
????????'查找銷售員是鄧和姓張的銷售員合同
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
????????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(en._合同編號).?_
????????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("鄧"))?_
????????????????????????????????????????????????????).?_
????????????????????????????????????????????????????_Or?_
????????????????????????????????????????????????????(en._銷售員.Filter.StartWith("張"))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
'Like包含
??????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員?Like?@pcb75950fbe764c2ba94112ecbaac7eb5
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.Include("山"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) 'Like右匹配
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員?Like?@p6ccb3db2ac984a4ea1d908c09dde5464
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.EndWith("公司"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ? 'Like字段左匹配
??'SELECT?XSH_HT_合同.乙方代表,XSH_HT_合同.銷售員?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.乙方代表?Like?XSH_HT_合同.銷售員+'%'
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._乙方代表,?en._銷售員).From(en).WHERE(en._乙方代表.Filter.StartWith(en._銷售員))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ?? '字段相等
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=XSH_HT_合同.乙方代表
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._銷售員,?en._乙方代表).From(en).WHERE(en._銷售員.Filter.EqualTo(en._乙方代表))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ?? '字段等于值
????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pe72d31922f434cd89abbadd51d2cf566
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.EqualTo("鄧"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) NOt IN
?'SELECT??*??FROM?XSH_HT_合同?WHERE??NOT?XSH_HT_合同.銷售員?IN??(@pe8b92721ff8c42548fb96965da962a9a)
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.NotIn("鄧山"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter.NotIn(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("鄧山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) exists
????????'SELECT??*??FROM?XSH_HT_工程信息?WHERE??EXISTS??(?SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_工程信息.合同編號=XSH_HT_合同.合同編號)
????????Dim?cp?As?New?XSH_HT_工程信息
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(cp).WHERE(cp.Exists(cp._合同編號.Filter.EqualTo(en._合同編號),?en))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
????????Dim?cp?As?New?XSH_HT_工程信息
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(cp).WHERE(cp.NotExists(cp._合同編號.Filter.EqualTo(en._合同編號),?en))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
IN語句
?'SELECT?XSH_HT_合同.合同編號,XSH_HT_合同.合同名稱?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(@p70798e19b6c748519ab37515e1884d8b,@pd0873785429445d8868c51dfd310b31f,@p7d84bb7aa5e340909e02527948ca63c7)?GROUP?BY?XSH_HT_合同.合同編號,XSH_HT_合同.合同名稱?ORDER?BY?XSH_HT_合同.合同編號?ASC
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._合同編號,?en._合同名稱).?_
?????????????????????????????????????????????????????????????????????????From(en).WHERE(en._合同編號.Filter._In("020482",?"020503",?"020539")).?_
?????????????????????????????????????????????????????????????????????????GroupBy(en._合同編號,?en._合同名稱).?_
?????????????????????????????????????????????????????????????????????????OrderBy(en._合同編號.OrderBy(SortDirection.DESC))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
IN語句
???????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pc93cea79f9884d15ae19367bdcab5d0c)
????????'使用IN?語句返回所有[鄧山]的合同.
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) IN語句
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pc93cea79f9884d15ae19367bdcab5d0c)
????????'使用IN?語句返回所有[鄧山]的合同.
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
? '左匹配
?'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@p382468e15c184543b73bbd0434f8440a)?OR?XSH_HT_合同.銷售員?Like?@p1b7b18f217a846e2ae7391320326e857
????????'使用IN?和?Like?語句
????????'查找銷售員是鄧和姓張的銷售員合同
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter._In(?_
????????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(en._合同編號).?_
????????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("鄧"))?_
????????????????????????????????????????????????????).?_
????????????????????????????????????????????????????_Or?_
????????????????????????????????????????????????????(en._銷售員.Filter.StartWith("張"))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
'Like包含
??????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員?Like?@pcb75950fbe764c2ba94112ecbaac7eb5
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.Include("山"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) 'Like右匹配
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員?Like?@p6ccb3db2ac984a4ea1d908c09dde5464
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.EndWith("公司"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ? 'Like字段左匹配
??'SELECT?XSH_HT_合同.乙方代表,XSH_HT_合同.銷售員?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.乙方代表?Like?XSH_HT_合同.銷售員+'%'
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._乙方代表,?en._銷售員).From(en).WHERE(en._乙方代表.Filter.StartWith(en._銷售員))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ?? '字段相等
???'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=XSH_HT_合同.乙方代表
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select(en._銷售員,?en._乙方代表).From(en).WHERE(en._銷售員.Filter.EqualTo(en._乙方代表))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) ?? '字段等于值
????'SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@pe72d31922f434cd89abbadd51d2cf566
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.EqualTo("鄧"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) NOt IN
?'SELECT??*??FROM?XSH_HT_合同?WHERE??NOT?XSH_HT_合同.銷售員?IN??(@pe8b92721ff8c42548fb96965da962a9a)
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(en).WHERE(en._銷售員.Filter.NotIn("鄧山"))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
not select
????????'SELECT??*??FROM?XSH_HT_合同?WHERE??NOT?XSH_HT_合同.合同編號?IN??(?SELECT??DISTINCT?XSH_HT_合同.合同編號?FROM?XSH_HT_合同?WHERE?XSH_HT_合同.銷售員=@p925776458a204a4d9999a1fae33470ec)????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().?_
????????????????????????From(en).?_
????????????????????????WHERE(en._合同編號.Filter.NotIn(?_
?????????????????????????????????????????????????????Lily.OQL.BuilderSql.Select(0,?True,?en._合同編號).?_
????????????????????????????????????????????????????From(en).?_
????????????????????????????????????????????????????WHERE(en._銷售員.Filter.EqualTo("鄧山")))?_
???????????????????????????????)
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd) exists
????????'SELECT??*??FROM?XSH_HT_工程信息?WHERE??EXISTS??(?SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_工程信息.合同編號=XSH_HT_合同.合同編號)
????????Dim?cp?As?New?XSH_HT_工程信息
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(cp).WHERE(cp.Exists(cp._合同編號.Filter.EqualTo(en._合同編號),?en))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
not exists
??'SELECT??*??FROM?XSH_HT_工程信息?WHERE??NOT?EXISTS??(?SELECT??*??FROM?XSH_HT_合同?WHERE?XSH_HT_工程信息.合同編號=XSH_HT_合同.合同編號)????????Dim?cp?As?New?XSH_HT_工程信息
????????Dim?cmd?As?Lily.PL.PersistenceLayerCommandBase?=?Lily.OQL.BuilderSql.Select().From(cp).WHERE(cp.NotExists(cp._合同編號.Filter.EqualTo(en._合同編號),?en))
????????Me.DataGrid1.DataSource?=?da.GetDataTable(cmd)
歡迎各位拍磚,這樣的查詢方式,簡便明了嗎?
XSH_HT_工程信息 是實體對象
PersistenceLayerCommandBase 是命令對象所有的select,update,delete,insert into 語句的構(gòu)造都是從此類派生.
da 是一個操作命令對象的適配器.
轉(zhuǎn)載于:https://www.cnblogs.com/zqonline/archive/2006/09/14/503714.html
總結(jié)
以上是生活随笔為你收集整理的SQL语句对象化,先看示例代码.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JOURNAL】911的虚惊
- 下一篇: 人与OO