如果为Null,请检查每个List项

时间:2016-01-18 14:37:30

标签: c# .net web-services sharepoint

我很难编写代码,因此问题。下面我将从DB中将项目添加到SharePoint列表中,同时检查是否有任何字段包含DBNull。而不是必须像我下面那样对每一列进行检查,有人可以帮我把它放在一个循环中吗?我将永远感激

using (OdbcConnection connection = new OdbcConnection())
{
    connection.ConnectionString = "dsn=abc;uid=xyz;pwd=yuo;DataSource=aaa";
    connection.ConnectionTimeout = 100;
    connection.Open();

    OdbcCommand command_donor = new OdbcCommand("Select * From vw_SP_aaa_bbb", connection);

    try
    {
        using (OdbcDataReader reader = command_donor.ExecuteReader())
        {
            while (reader.Read())
            {

                var obj0 = reader.GetValue(48);
                var obj1 = reader.GetValue(0);
                var obj2 = reader.GetValue(33);
                var obj3 = reader.GetValue(47);
                var obj4 = reader.GetValue(42);
                var obj5 = reader.GetValue(42);


                ListItem oListItem_aaa = oList_aaa .AddItem(itemCreateInfo);


                if (obj0 == null || obj0.Equals(DBNull.Value))
                { oListItem_aaa["Title"] = ""; }
                else
                { oListItem_aaa["Title"] = reader.GetString(48).ToString(); }

                if (obj1 == null || obj1.Equals(DBNull.Value))
                { oListItem_aaa["aaa_x0020_ID"] = ""; }
                else
                { oListItem_aaa["aaa_x0020_ID"] = reader.GetString(0).ToString(); }

                if (obj2 == null || obj2.Equals(DBNull.Value))
                { oListItem_aaa["Excluded_x0020_By"] = ""; }
                else
                { oListItem_aaa["Excluded_x0020_By"] = reader.GetString(33).ToString(); }

                if (obj3 == null || obj3.Equals(DBNull.Value))
                { oListItem_aaa["Excluded_x0020_On"] = ""; }
                else
                { oListItem_aaa["Excluded_x0020_On"] = reader.GetDateTime(47).ToString("MM/dd/yyyy"); }

                if (obj4 == null || obj4.Equals(DBNull.Value))
                { oListItem_aaa["Reason"] = ""; }
                else
                { oListItem_aaa["Reason"] = reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length-50); }

                if (obj5 == null || obj5.Equals(DBNull.Value))
                { oListItem_aaa["Publish"] = ""; }
                else
                { oListItem_aaa["Publish"] = reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length - 50); }

                oListItem_aaa.Update();

                context.ExecuteQuery();

            }

        }

    }

    catch (Exception exception)
    {
        Console.WriteLine("The Error is:" + exception);
        Console.ReadLine();
    }

}

1 个答案:

答案 0 :(得分:1)

使用短期IIF

oListItem_aaa["Excluded_x0020_By"] = (obj1 == null || obj1.Equals(DBNull.Value)) ? "" : reader.GetString(0).ToString();

var obj0 = (reader.GetValue(48) != DBNull.Value && !String.IsNullOrEmpty(Convert.ToString(reader.GetValue(48)))) ? reader.GetValue(48) : "");

在一行。然后你不需要使用if then else的...在Operator ?: (C#-Referenz)的MSDN中也有描述。