从 SQL 查询设置对象值
Posted
技术标签:
【中文标题】从 SQL 查询设置对象值【英文标题】:Setting Object values from SQL Query 【发布时间】:2017-05-25 20:25:09 【问题描述】:我正在尝试遍历 SQL 查询并将对象参数设置为特定值。似乎工作正常的唯一值是 workpack.JobCardIDs,因为我可以实现一个 foreach 循环来显示结果。例如,如果我尝试将 Label 的 Text 属性设置为 workpack.WorkPackTitle,即使数据库值是每一行的值,它也会显示空白。
我对 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()) 行上设置断点,它会进入循环还是完全跳过循环? 它进入循环内部,遍历循环并将记录添加到 jobCardIDs 列表。然后,当在方法之外我可以调用 workpack.JobCardIDs 并且可以读取它。工作包对象的所有其他元素都是空白的。这是一个奇怪的。 【参考方案1】:看起来您从未创建 jobCardIDs 列表的本地实例。您需要在 ExecuteReader 块内执行此操作。见下文。总账
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())
List<int> jobCardIDs = new List<int>(); //<--***THIS IS THE LINE YOU NEED TO ADD***
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;
【讨论】:
【参考方案2】:社区之前给出的答案并没有解决问题,尽管我确实添加了额外的 sn-p 。
问题在于创建对象并在回传之间传递。该对象将与选择作业卡选项卡相关,但一旦加载页面,就没有代码来重建该实例。
添加
Object foo = new Object();
在 page_Load() 中并重建解决了该问题。如果有人对如何让实例保持活力有任何建议,我完全赞成。我认为 ViewState() 和 Session[] 都是适用的方法。
【讨论】:
以上是关于从 SQL 查询设置对象值的主要内容,如果未能解决你的问题,请参考以下文章