从SQL查询设置对象值

时间:2017-05-25 20:25:09

标签: sql asp.net sql-server class object

我正在尝试迭代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;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您似乎永远不会创建jobCardIDs列表的本地实例。您只想在ExecuteReader块中执行此操作。见下文。 GL

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;
            }
        }
    }
}

答案 1 :(得分:0)

社区提供的先前答案并没有解决问题,尽管我确实把它添加了一些代码片段。

问题在于创建对象并在回发之间传递。该对象与选择作业卡选项卡相关,但一旦加载的页面没有重建该实例的代码。

添加

      Object foo = new Object();

在page_Load()中并重建修复了问题。如果有人对如何让一个实例保持活着有任何建议,我就是全部听到它。我认为ViewState()和Session []都是适用的方法。