1
我在編寫代碼方面很差,因此存在問題。下面我將從DB中將項目添加到SharePoint列表中,同時檢查是否有任何字段包含DBNulls。如果可能,有人可以幫我把它放在一個循環中,而不是像我下面那樣對每一列執行檢查。我會永遠感激檢查每個列表項目是否爲
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();
}
}
而不是一個循環,我會建議編寫一個處理重複邏輯的方法。或者,您也可以更改查詢以合併空值。 – juharr
我也建議在Select語句中添加一個where子句來篩選出你知道會返回Null值的記錄和或數據。爲了可讀性,我也不會使用'GetString'方法..如果列順序發生變化..?你應該訪問'reader [「FieldName」]。ToString()'這種方式使讀取更容易 – MethodMan
juharr:感謝您的評論。請給我看一個我可以遵循的例子嗎?我知道我問得太多了,但這會非常有幫助 – SharePointDummy