修剪ADO.net数据表中的字符串字段 [英] Trim string fields in ADO.net datatable

查看:63
本文介绍了修剪ADO.net数据表中的字符串字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过SqldataAdapter填充ADO.Net表时,有没有办法自动删除所有空格?



我想留下ansi填充为我的SQL服务器和我想阻止在我的代码中单独修剪每个datarow字段或datarowview字段。



有没有一种方法,dataadapter或bindingsource可以做到这一点通过中央设置自动为我。

Is there a way to automatically remove all whitespaces when I fill an ADO.Net table through an SqldataAdapter ?

I would like to leave ansi padding on for my SQL server and I would like to prevent to Trim each datarow field or datarowview field individually in my code.

Is there a way that the dataadapter or the bindingsource can do this for me automatically, through a central setting.

推荐答案

在这种情况下,您可以将自己的扩展方法添加到您的数据流类。

传递datarow,循环遍历所有datarow字段,编写代码以删除空格。

您可以创建一个名为Fliters的单独文件夹,并添加类DataRowFilter,在其中编写方法RemoveWhiteSpaces()



代码执行后如下:



Well in that case you can add your own extension method to your datarow class.
Pass datarow, loop through all datarow fields, write code to remove white spaces.
You can create a separate folder called as Fliters and add class DataRowFilter in which you write a method RemoveWhiteSpaces()

Code After such implementation will look like:

DataRow row;
row = tableName.NewRow();
// Then add the new row to the collection.
row["greeting"] = "Hello World   ";
row["text"] = "How are you?   ";
row.RemoveWhiteSpaces(); //After implementation you will get this method available for datarow class.
tableName.Rows.Add(row);
//Persist at DB side.





扩展方法文章链接:扩展方法C#



优势:

1)数据过滤方法。

2)每当DataRow将在你的dll中创建,你可以使用这个方法。



Extension method article link: Extension Methods C#

Advantage:
1) Data filteration method.
2) Whenever DataRow will be created in your dll, you can use this method.


我自己已经创建了这个sub,它可以在DataAdapter填满表后调用。然而,性能权衡太大(特别是对于较大的表格)。



I already made this sub myself, which can be called after the DataAdapter has filled the table. However the performance tradeoff is too much (especially with larger tables).

Friend Sub Trim_DataTable(dt As DataTable)
    For Each Row As DataRow In dt.Rows
        For Each Col As DataColumn In dt.Columns
            If Col.DataType = System.Type.GetType("System.String") Then
                If IsDBNull(Row.Item(Col.ColumnName)) Then
                    Row.Item(Col.ColumnName) = ""
                Else
                    Row.Item(Col.ColumnName) = Row.Item(Col.ColumnName).trim
                End If
            End If
        Next
    Next
End Sub


这篇关于修剪ADO.net数据表中的字符串字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆