简体   繁体   English

如何将DataTable中的空数据设​​置为空字符串

[英]How to set null data in DataTable to empty string

I populated datatable object with data from database, and I need to check if some of them are NULL and set it to empty string .我居住datatable对象与数据库中的数据,我需要检查,如果他们中的一些是NULL并将其设置为空字符串
The problem is that some data are NULL in database but they are "0" in DataTable.问题是某些数据在数据库中为NULL ,但在 DataTable 中为“0”
I have tried to set to "" , String.EmptyString etc. nothing works.我试图设置为""String.EmptyString等。没有任何效果。

foreach (DataRow row in ds.Tables[0].Rows)
{
    foreach (DataColumn c in ds.Tables[0].Columns)
    {
        object value = row[c];

        if (value == DBNull.Value)
        {
            try
            {
                row[c] = "";

try changing to;尝试更改为;

foreach (DataRow row in ds.Tables[0].Rows)
{
    foreach (DataColumn c in ds.Tables[0].Columns)
    {
        if (row.IsNull(c))
        {
            try
            {
                row[c] = string.Empty;

The row.IsNull(c) check is more reliable than directly comparing to DBNull.Value as it can evaluate more than one thing as equivilent to NULL in the DB. row.IsNull(c)检查比直接与DBNull.Value进行比较更可靠,因为它可以评估不止一件事与数据库中的NULL等效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM