微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DataTable DataRow String Tips...

2012-2-6 17:32:24 PM IS2120@CSDN
与datatable奋战了一天,记录一下。。。

1. 查看得到的 datatable 是否为空 datatable.Rows.Count
2. 查看得到的 DaTarow[] 是否为空,可用 DaTarow.Length
3. DataTable 在进行select的时候,认是 CaseSensitive 为 false
4. string.IndexOf 认是区分大小写的
可以通过参数 System.StringComparison.OrdinalIgnoreCase ,忽略大小写
另有System.StringComparison.CurrentCultureIgnoreCase等

you Could use IndexOf() and pass StringComparison.OrdinalIgnoreCase
string title = "STRING"; bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0; 

Even better is defining a new extension method for string

public static bool Contains(this string source, string tocheck, StringComparison comp) {  return source.IndexOf(tocheck, comp) >= 0; } string title = "STRING"; bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase); 

一个解决方案:
bool contains = Regex.Match("StRiNG to search", "string", RegexOptions.IgnoreCase).Success;
2012-2-6 17:32:24 PM IS2120@CSDN
5. 在 datatable 上使用通配
myDataTable.Select( "Title like '*Professional* ' ")
或者myDataTable.Select( "Title like '%Professional% ' ")

6. 在使用Select方法或DataView的时候,一定要注意把字符条件值的一个单引号改成两个单引号,执行
str = str.Replace("'","''");!!
http://www.cnblogs.com/hjf1223/archive/2005/08/31/227170.html
用DataTable.Select(string)或给DataView.RowFilter设置Expression表达式时,由于Expression是字符串拼接而成的,因为跟sql语句也要注意单引号问题.如这个的查询会导致异常的发生:
DaTarow[]m_drResult = dt.Select( " name='name's' " );
解决办法是将一个单引号变成两个(跟sql语法是一样的).
DaTarow[]m_drResult = m_dtSource.Select( " name='name''s' " )

2012-2-7 13:52:53 PM IS2120@CSDN
一个解决方法
http://linkcd.cnblogs.com/archive/2005/08/31/227276.html
看到§猪阿不猪§提到一个DataTable.Select的注意事项: 注意去掉不正确的单引号.

平时项目中,我们一般是直接在写filter语句时这样写

theName = theName.Replace("'","''");
string filter = string.Format("Name = '{0}'",theName);

不过有时候也比较麻烦,如果你的filter里面code的值一多,处理起来就比较烦.

针对这个问题,以前写了一个专门的replace函数(vb.net),能够智能替换filter中不正确的单引号,并保留正确的单引号.

比如有语句: filter = "Name = 'theN'ame' AND Code = 'd'd'",把整个filter经过处理之后,得到"Name = 'theN''ame' AND Code = 'd''d'"

不过这个版本的算法比较粗糙 (特别时在处理Like,IN关键字的时候,实在是恶心的代码 -_-bb) 不过暂时能应付项目的要求,当然Unit Test也是必不可少的.

大家可以看看,也欢迎提出更好的算法 :)

代码和Unit Test 代码这里.(updated: 2005/9/23: fixed some bugs)

最后废话一句: 用Dataset就是麻烦...


7. 向datatable添加 daTarow
northwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();

newCustomersRow.CustomerID = "ALFKI";
newCustomersRow.CompanyName = "Alfreds Futterkiste";

northwindDataSet1.Customers.Rows.Add(newCustomersRow);

//z 2012-3-27 15:57:39 PM IS2120@CSDN
8. 向一个表(dstTB)加入一行已属于另一个table(srcDT)的一个daTarow时使用 ImportRow
This row already belongs to another table.


9. .net 4 中添加了这个类 System.Runtime.Caching.MemoryCache ,可以用来做cache

原文地址:https://www.jb51.cc/vb/260362.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐