Return objects with populated list properties from stored procedure

后端 未结 2 1700
深忆病人
深忆病人 2021-01-26 09:10

I\'m new to SQL Server stored procedures, so apologies if I\'m being an idiot. I would like to use a stored procedure to return a list of objects, each of which has a property c

2条回答
  •  情书的邮戳
    2021-01-26 10:06

    Here is your most basic type of ORM mapper.

    Well, most basic, with some maintainability and readability in mind.

    I would hit the database ONCE, but have multiple resultsets in your stored procedure. And look into the IDataReader.NextResult

    (as seen here LINK )

    Below is some basic ORM.

    [Serializable]
    public partial class Answer
    {
        public int AnswerKey { get; set; }                   
        public int ParentQuestionID { get; set; }
        public string AnswerText { get; set; }                   
    
        public Question ParentQuestion  { get; set; }    
    
    }
    
    internal static class AnswerDefaultLayout
    {
        public static readonly int AnswerKey = 0;
        public static readonly int ParentQuestionID = 1;
        public static readonly int AnswerText = 2;
    
    }
    
    public class AnswerSerializer
    {
        public ICollection SerializeAnswers(IDataReader dataReader)
        {
            Answer item = new Answer();
            ICollection returnCollection = new List();
    
                int fc = dataReader.FieldCount;//just an FYI value
    
                int counter = 0;//just an fyi of the number of rows
    
                while (dataReader.Read())
                {
    
                    if (!(dataReader.IsDBNull(AnswerDefaultLayout.AnswerKey)))
                    {
                        item = new Answer() { AnswerKey = dataReader.GetInt32(AnswerDefaultLayout.AnswerKey) };
    
                        if (!(dataReader.IsDBNull(AnswerDefaultLayout.ParentQuestionID)))
                        {
                            item.ParentQuestionID = dataReader.GetInt32(AnswerDefaultLayout.ParentQuestionID);
                        }
    
                        if (!(dataReader.IsDBNull(AnswerDefaultLayout.AnswerText)))
                        {
                            item.AnswerText = dataReader.GetString(AnswerDefaultLayout.AnswerText);
                        }
    
    
                        returnCollection.Add(item);
                    }
    
                    counter++;
                }
    
                return returnCollection;
    
        }
        }
    
    
    [Serializable]
    public class Question
    {
        public int QuestionID { get; set; }
        public string Question { get; set; }
        public ICollection Answers { get; set; }
    }
    
    internal static class QuestionDefaultLayout
    {
        public static readonly int QuestionID = 0;
        public static readonly int QuestionText = 1;
    }
    
    
    public class QuestionSerializer
    {
        public ICollection SerializeQuestions(IDataReader dataReader)
        {
            Question item = new Question();
            ICollection returnCollection = new List();
    
    
                int fc = dataReader.FieldCount;//just an FYI value
    
                int counter = 0;//just an fyi of the number of rows
    
                while (dataReader.Read())
                {
    
                    if (!(dataReader.IsDBNull(QuestionDefaultLayout.QuestionID)))
                    {
                        item = new Question() { QuestionID = dataReader.GetInt32(QuestionDefaultLayout.QuestionID) };
    
                        if (!(dataReader.IsDBNull(QuestionDefaultLayout.LAST_NAME)))
                        {
                            item.LastName = dataReader.GetString(QuestionDefaultLayout.LAST_NAME);
                        }
    
    
    
                        returnCollection.Add(item);
                    }
    
                    counter++;
                }
    
                return returnCollection;
    
    
        }
    }
    
    
    
    
    
    
    public class QuestionManager
    {
    
        public ICollection GetAllQuestionsWithChildAnswers()
        {
    
        String myConnString  = "User ID=;password=;Initial Catalog=pubs;Data Source=myServer";
        SqlConnection myConnection = new SqlConnection(myConnString);
        SqlCommand myCommand = new SqlCommand();
        SqlDataReader myReader ;
    
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Connection = myConnection;
        myCommand.CommandText = "dbo.uspQuestionAndAnswersGetAll";
        int RecordCount=0; 
    
        try
        {
            myConnection.Open();
            myReader = myCommand.ExecuteReader();
    
            ICollection questions = new QuestionSerializer().SerializeQuestions(myReader);
    
            myReader.NextResult();
    
            ICollection answers = new AnswerSerializer().SerializeAnswers(myReader);
    
            questions = this.MergeQuestionObjectGraphs(questions, answers);
    
        catch(Exception ex) 
        {
           MessageBox.Show(ex.ToString());
        }
        finally
        {
        if (null != myReader)
        {
            myReader.Close();
        }
        if (null != myConnection)
        {
            myConnection.Close();
        }
        }
        }
    
            private ICollection MergeQuestionObjectGraphs(ICollection qtions, ICollection aners)
            {
                if (null != qtions && null != aners)
                {
                    foreach (Question qtn in qtions)
                    {
                        IEnumerable foundLinks = aners.Where(lnk => lnk.ParentQuestionId == qtn.QuestionId);
                        if (null != foundLinks)
                        {
                            foreach (Answer link in foundLinks)
                            {
                                link.ParentQuestion = qtn;
                            }
    
                            qtn.Answers = foundLinks.ToList();
                        }
                    }
                }
    
                return qtions;
            }
    
    }
    

    TSQL

    CREATE PROC dbo.uspQuestionAndAnswersGetAll
    AS
        SELECT QuestionId, QuestionText FROM dbo.Question
        SELECT AnswerId, QuestionId, AnswerText FROM dbo.Answer
    GO 
    

提交回复
热议问题