コード例 #1
0
 public static string ToJsonString(ProfessorsWithAnswersModel model)
 {
     if (model == null)
     {
         return(null);
     }
     else
     {
         return(JsonConvert.SerializeObject(model));
     }
 }
コード例 #2
0
        public static ProfessorsWithAnswersModel GetProfessorsWithAnswers(string department)
        {
            ProfessorsWithAnswersModel professors = null;
            SqlConnection connection = GetConnection();

            string selectStatement = "EXEC ProfessorsWithAnswers @dept";

            SqlCommand command = new SqlCommand(selectStatement, connection);

            command.Parameters.AddWithValue("dept", department);

            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                professors = new ProfessorsWithAnswersModel();
                while (reader.Read())
                {
                    var data = new ProfessorsWithAnswersModel.Data();
                    data.instructorid = Convert.ToInt32(reader["instructorID"].ToString());
                    data.firstname    = reader["FirstName"].ToString();
                    data.lastname     = reader["LastName"].ToString();


                    professors.DATA.Add(data);
                }

                return(professors);
            }

            catch (SqlException ex)
            {
                return(null);
            }


            finally
            {
                connection.Close();
            }
        }