zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

DataView.RowFilter的使用(包括in,like等SQL中的操作符)

SQL 使用 in 包括 操作符 like
2023-06-13 09:14:29 时间

DataViewRowFilterSyntax[C#]
ThisexampledescribessyntaxofDataView.RowFilterexpression.Itshowshowtocorrectlybuildexpressionstring(without„SQLinjection“)usingmethodstoescapevalues.

Columnnames
Ifacolumnnamecontainsanyofthesespecialcharacters~()#//=><+-*%&|^""[],youmustenclosethecolumnnamewithinsquarebrackets[].Ifacolumnnamecontainsrightbracket]orbackslash/,escapeitwithbackslash(/]or//).

[C#]

dataView.RowFilter="id=10";//nospecialcharacterincolumnname"id"dataView.RowFilter="$id=10";//nospecialcharacterincolumnname"$id"dataView.RowFilter="[#id]=10";//specialcharacter"#"incolumnname"#id"dataView.RowFilter="[[id/]]=10";//specialcharactersincolumnname"[id]"
Literals
Stringvaluesareenclosedwithinsinglequotes"".Ifthestringcontainssinglequote",thequotemustbedoubled.

[C#]

dataView.RowFilter="Name="John""//stringvaluedataView.RowFilter="Name="John""A""""//stringwithsinglequotes"John"A""dataView.RowFilter=String.Format("Name="{0}"","John"A"".Replace(""",""""));
Numbervaluesarenotenclosedwithinanycharacters.Thevaluesshouldbethesameasistheresultofint.ToString()orfloat.ToString()methodforinvariantorEnglishculture.

[C#]

dataView.RowFilter="Year=2008"//integervaluedataView.RowFilter="Price=1199.9"//floatvaluedataView.RowFilter=String.Format(CultureInfo.InvariantCulture.NumberFormat,"Price={0}",1199.9f);
Datevaluesareenclosedwithinsharpcharacters##.ThedateformatisthesameasistheresultofDateTime.ToString()methodforinvariantorEnglishculture.

[C#]

dataView.RowFilter="Date=#12/31/2008#"//datevalue(timeis00:00:00)dataView.RowFilter="Date=#2008-12-31#"//alsothisformatissupporteddataView.RowFilter="Date=#12/31/200816:44:58#"//dateandtimevaluedataView.RowFilter=String.Format(CultureInfo.InvariantCulture.DateTimeFormat,"Date=#{0}#",newDateTime(2008,12,31,16,44,58));
Alternativelyyoucanencloseallvalueswithinsinglequotes"".Itmeansyoucanusestringvaluesfornumbersordatetimevalues.Inthiscasethecurrentcultureisusedtoconvertthestringtothespecificvalue.

[C#]

dataView.RowFilter="Date="12/31/200816:44:58""//ifcurrentcultureisEnglishdataView.RowFilter="Date="31.12.200816:44:58""//ifcurrentcultureisGermandataView.RowFilter="Price="1199.90""//ifcurrentcultureisEnglishdataView.RowFilter="Price="1199,90""//ifcurrentcultureisGerman
Comparisonoperators
Equal,notequal,less,greateroperatorsareusedtoincludeonlyvaluesthatsuittoacomparisonexpression.Youcanusetheseoperators=<><<=>>=.

Note:Stringcomparisonisculture-sensitive,itusesCultureInfofromDataTable.Localepropertyofrelatedtable(dataView.Table.Locale).Ifthepropertyisnotexplicitlyset,itsdefaultvalueisDataSet.Locale(anditsdefaultvalueiscurrentsystemcultureThread.CurrentThread.CurrentCulture).

[C#]

dataView.RowFilter="Num=10"//numberisequalto10dataView.RowFilter="Date<#1/1/2008#"//dateislessthan1/1/2008dataView.RowFilter="Name<>"John""//stringisnotequalto"John"dataView.RowFilter="Name>="Jo""//stringcomparison
OperatorINisusedtoincludeonlyvaluesfromthelist.Youcanusetheoperatorforalldatatypes,suchasnumbersorstrings.

[C#]

dataView.RowFilter="IdIN(1,2,3)"//integervaluesdataView.RowFilter="PriceIN(1.0,9.9,11.5)"//floatvaluesdataView.RowFilter="NameIN("John","Jim","Tom")"//stringvaluesdataView.RowFilter="DateIN(#12/31/2008#,#1/1/2009#)"//datetimevaluesdataView.RowFilter="IdNOTIN(1,2,3)"//valuesnotfromthelist
OperatorLIKEisusedtoincludeonlyvaluesthatmatchapatternwithwildcards.Wildcardcharacteris*or%,itcanbeatthebeginningofapattern"*value",attheend"value*",oratboth"*value*".Wildcardinthemiddleofapatern"va*lue"isnotallowed.

[C#]

dataView.RowFilter="NameLIKE"j*""//valuesthatstartwith"j"dataView.RowFilter="NameLIKE"%jo%""//valuesthatcontain"jo"dataView.RowFilter="NameNOTLIKE"j*""//valuesthatdon"tstartwith"j"
IfapatterninaLIKEclausecontainsanyofthesespecialcharacters*%