您好,登錄后才能下訂單哦!
今天小編給大家分享一下C#怎么實(shí)現(xiàn)定義一套中間SQL可以跨庫執(zhí)行的SQL語句的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
根據(jù)hisql語句通過ToSql()方法生成目標(biāo)sql的原生sql
var _sql = sqlClient.HiSql("select * from HTest01 where CreateTime>='2022-02-17 09:27:50' and CreateTime<='2022-03-22 09:27:50'").ToSql();
hisql生成的sqlserver 的sql 如下的所示
select [htest01].[SID],[htest01].[UName],[htest01].[Age],[htest01].[Salary],[htest01].[Descript],[htest01].[CreateTime],[htest01].[CreateName],[htest01].[ModiTime],[htest01].[ModiName] from [HTest01] as [HTest01] where [htest01].[CreateTime] >= '2022-02-17 09:27:50.000' and [htest01].[CreateTime] <= '2022-03-22 09:27:50.000'
hisql生成mysql的sql如下所示
select `htest01`.`SID`,`htest01`.`UName`,`htest01`.`Age`,`htest01`.`Salary`,`htest01`.`Descript`,`htest01`.`CreateTime`,`htest01`.`CreateName`,`htest01`.`ModiTime`,`htest01`.`ModiName` from `htest01` as `htest01` where `htest01`.`CreateTime` >= '2022-02-17 09:27:50.000' and `htest01`.`CreateTime` <= '2022-03-22 09:27:50.000'
hisql生成postgresql 的sql下所示
select "htest01"."SID","htest01"."UName","htest01"."Age","htest01"."Salary","htest01"."Descript","htest01"."CreateTime","htest01"."CreateName","htest01"."ModiTime","htest01"."ModiName" from "HTest01" as "htest01" where "htest01"."CreateTime" >= '2022-02-17 09:27:50.000' and "htest01"."CreateTime" <= '2022-03-22 09:27:50.000'
大家可以會(huì)問 明明是 "select * from HTest01 where CreateTime>='2022-02-17 09:27:50' and CreateTime<='2022-03-22 09:27:50'"
用的 *
查詢 生成的sql語句為什么會(huì)直接列出所有字段呢? 熟HiSql的網(wǎng)友都應(yīng)該知道這里其實(shí)隱藏了一個(gè)功能就是用戶可以自定義字段排序在系統(tǒng)表 Hi_FieldModel
中可配置
hisql 默認(rèn)支持的常用函數(shù) max
, count
, min
, sum
, avg
string sql = sqlClient.HiSql("select FieldName, count(FieldName) as NAME_count,max(FieldType) as FieldType_max from Hi_FieldModel group by FieldName").ToSql();
hisql生成的sqlserver 的sql 如下的所示
select [hi_fieldmodel].[FieldName],count(*) as NAME_count,max([hi_fieldmodel].[FieldType]) as FieldType_max from [Hi_FieldModel] as [Hi_FieldModel] group by [hi_fieldmodel].[FieldName]
hisql生成mysql的sql如下所示
select `hi_fieldmodel`.`FieldName`,count(*) as NAME_count,max(`hi_fieldmodel`.`FieldType`) as FieldType_max from `Hi_FieldModel` as `hi_fieldmodel` group by `hi_fieldmodel`.`FieldName`
hisql生成postgresql 的sql下所示
select "hi_fieldmodel"."FieldName",count(*) as NAME_count,max("hi_fieldmodel"."FieldType") as FieldType_max from "Hi_FieldModel" as "hi_fieldmodel" group by "hi_fieldmodel"."FieldName"
上次有網(wǎng)友說having 實(shí)現(xiàn)起來很麻煩通過正則表達(dá)式解析很難實(shí)現(xiàn),我只能說很多網(wǎng)友對(duì)正則表達(dá)式的理解還不是很深,正則表達(dá)式已經(jīng)應(yīng)用于種類型的編譯器中,下面就來演示一下Having
string sql_having = sqlClient.HiSql("select FieldName, count(FieldName) as NAME_count,max(FieldType) as FieldType_max from Hi_FieldModel group by FieldName having count(FieldName) > 1").ToSql();
hisql生成的sqlserver 的sql 如下的所示
select [hi_fieldmodel].[FieldName],count(*) as NAME_count,max([hi_fieldmodel].[FieldType]) as FieldType_max from [Hi_FieldModel] as [Hi_FieldModel] group by [hi_fieldmodel].[FieldName] having count(*) > 1
hisql生成mysql的sql如下所示
select `hi_fieldmodel`.`FieldName`,count(*) as NAME_count,max(`hi_fieldmodel`.`FieldType`) as FieldType_max from `Hi_FieldModel` as `hi_fieldmodel` group by `hi_fieldmodel`.`FieldName` having count(*) > 1
hisql生成postgresql 的sql下所示
select "hi_fieldmodel"."FieldName",count(*) as NAME_count,max("hi_fieldmodel"."FieldType") as FieldType_max from "Hi_FieldModel" as "hi_fieldmodel" group by "hi_fieldmodel"."FieldName" having count(*) > 1
hisql inner join 和 in 操作語法
var sql = sqlClient.HiSql("select a.tabname from hi_fieldmodel as a inner join Hi_TabModel as b on a.tabname =b.tabname inner join Hi_TabModel as c on a.tabname=c.tabname where a.tabname='h_test' and a.FieldType in (11,41,21) ").ToSql();
hisql生成的sqlserver 的sql 如下的所示
select [a].[TabName] from [Hi_FieldModel] as [a] inner join [Hi_TabModel] as [b] on [a].[TabName] = [b].[TabName] inner join [Hi_TabModel] as [c] on [a].[TabName] = [c].[TabName] where [a].[TabName] = 'h_test' and [a].[FieldType] in (11,41,21)
hisql生成mysql的sql如下所示
select `a`.`TabName` from `Hi_FieldModel` as `a` inner join `Hi_TabModel` as `b` on `a`.`TabName` = `b`.`TabName` inner join `Hi_TabModel` as `c` on `a`.`TabName` = `c`.`TabName` where `a`.`TabName` = 'h_test' and `a`.`FieldType` in (11,41,21)
hisql生成postgresql 的sql下所示
select "a"."TabName" from "Hi_FieldModel" as "a" inner join "Hi_TabModel" as "b" on "a"."TabName" = "b"."TabName" inner join "Hi_TabModel" as "c" on "a"."TabName" = "c"."TabName" where "a"."TabName" = 'h_test' and "a"."FieldType" in (11,41,21)
hisql 分頁 只要在日常查詢上增加 Take()
每頁顯示多少數(shù)據(jù) Skip()
顯示第幾頁
var sql = sqlClient.HiSql("select a.tabname from hi_fieldmodel as a inner join Hi_TabModel as b on a.tabname =b.tabname inner join Hi_TabModel as c on a.tabname=c.tabname where a.tabname='h_test' and a.FieldType in (11,41,21) ").Take(2).Skip(2).ToSql();
hisql生成的sqlserver 的sql 如下的所示
select [TabName] from ( select ROW_NUMBER() OVER(Order by [a].[FieldType] ASC) AS _hi_rownum_, [a].[TabName] from [Hi_FieldModel] as [a] inner join [Hi_TabModel] as [b] on [a].[TabName] = [b].[TabName] inner join [Hi_TabModel] as [c] on [a].[TabName] = [c].[TabName] where [a].[TabName] = 'h_test' and [a].[FieldType] in (11,41,21) ) as hi_sql where hi_sql._hi_rownum_ BETWEEN (2-1)*2+1 and 2*2 order by _hi_rownum_ asc
hisql生成mysql的sql如下所示
select `TabName` from ( select ROW_NUMBER() OVER(Order by `a`.`FieldType` ASC) AS `_hi_rownum_`, `a`.`TabName` from `Hi_FieldModel` as `a` inner join `Hi_TabModel` as `b` on `a`.`TabName` = `b`.`TabName` inner join `Hi_TabModel` as `c` on `a`.`TabName` = `c`.`TabName` where `a`.`TabName` = 'h_test' and `a`.`FieldType` in (11,41,21) ) as hi_sql order by `_hi_rownum_` asc limit 2,2
hisql生成postgresql 的sql下所示
select "TabName" from ( select ROW_NUMBER() OVER(Order by "a"."FieldType" ASC) AS "_hi_rownum_", "a"."TabName" from "Hi_FieldModel" as "a" inner join "Hi_TabModel" as "b" on "a"."TabName" = "b"."TabName" inner join "Hi_TabModel" as "c" on "a"."TabName" = "c"."TabName" where "a"."TabName" = 'h_test' and "a"."FieldType" in (11,41,21) ) as hi_sql order by "_hi_rownum_" asc limit 2 OFFSET 2
參數(shù)化可以有效的防注入,通過前端可以拼接的hisql語句通過參數(shù)化的方式傳入, 這樣就解決了注入問題
string sql1= sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname ", new { TabName="H_test" ,FieldName="DID"}).ToSql(); string sql2= sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname or TabType in( @TabType)", new { TabName="H_test" , TabType =new List<int> { 1,2,3,4} }).ToSql(); string sql3 = sqlClient.HiSql("select * from hi_tabmodel where tabname=@tabname ", new Dictionary<string, object> { { "TabName", "H_test" } }).ToSql();
hisql除了提供hisql語法外還提供鏈?zhǔn)讲樵兎椒ㄈ缦滤?/p>
string sql = sqlClient.Query("Hi_FieldModel").As("A").Field("A.FieldType") .Join("Hi_TabModel").As("B").On(new HiSql.JoinOn() { { "A.TabName", "B.TabName" } }) .Where("A.TabName='GD_UniqueCodeInfo'").Group(new GroupBy { { "A.FieldType" } }) .Sort("A.FieldType asc", "A.TabName asc") .Take(2).Skip(2) .ToSql();
string sql = sqlClient.HiSql("select A.FieldType from Hi_FieldModel as A ") .Where("A.TabName='GD_UniqueCodeInfo'").Group(new GroupBy { { "A.FieldType" } }) .Sort("A.FieldType asc", "A.TabName asc") .Take(2).Skip(2) .ToSql();
以上就是“C#怎么實(shí)現(xiàn)定義一套中間SQL可以跨庫執(zhí)行的SQL語句”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。