我正在嘗試遍歷SQL查詢並將對象參數設置爲特定值。似乎正常工作的唯一值是workpack.JobCardIDs,因爲我可以實現一個foreach循環來顯示結果。例如,如果我嘗試將Label的Text屬性設置爲workpack.WorkPackTitle,則即使數據庫值是針對每行的內容,也會顯示爲空白。從SQL查詢設置對象值
我對OOP相當陌生,所以不能完全確定是否有某些我缺少的東西是根本。
public class WorkPack
{
public int ID { get; set; }
public string WorkPackNumber { get; set; }
public string WorkPackTitle { get; set; }
public string WorkPackDescription { get; set; }
public Boolean IFC { get; set; }
public string SPA { get; set; }
public string Writer { get; set; }
public string Organization { get; set; }
public List<int> JobCardIDs { get; set; }
public int JobCard { get; set; }
}
public static WorkPack PopulateWorkPackObject(WorkPack workpack, int workPackID)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["vmdatamanagerConnectionString"].ConnectionString;
string sqlCall = "I HAVE REMOVED CALL BUT VERIFIED IT FUNCTIONS (SELECT columns FROM workpackdatabase where workpackname = x";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sqlCall, con))
{
cmd.Connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (reader.IsDBNull(reader.GetOrdinal("PARAM1")) == false)
workpack.WorkPackNumber = (reader.GetString(reader.GetOrdinal("PARAM1")));
if (reader.IsDBNull(reader.GetOrdinal("PARAM2")) == false)
workpack.WorkPackTitle = reader.GetString(reader.GetOrdinal("PARAM2"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM3")) == false)
workpack.WorkPackDescription = reader.GetString(reader.GetOrdinal("PARAM3"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM4")) == false)
workpack.IFC = reader.GetBoolean(reader.GetOrdinal("PARAM4"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM5")) == false)
workpack.SPA = reader.GetString(reader.GetOrdinal("PARAM5"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM6")) == false)
workpack.Writer = reader.GetString(reader.GetOrdinal("PARAM6"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM7")) == false)
workpack.Organization = reader.GetString(reader.GetOrdinal("PARAM7"));
if (reader.IsDBNull(reader.GetOrdinal("PARAM8")) == false)
jobCardIDs.Add(reader.GetInt32(reader.GetOrdinal("PARAM8")));
}
workpack.JobCardIDs = jobCardIDs;
return workpack;
}
}
}
}
如果你在while(reader.Read())行放置斷點,它是否進入循環內部,還是完全跳過它? – mjw
它進入循環內部,它遍歷循環並將記錄添加到jobCardID列表中。然後當方法外我可以調用workpack.JobCardIDs,它可以被讀取。工作包對象的每個其他元素都是空白的。它是一個奇怪的。 –