コード例 #1
0
 public static List<Speaker> GetSpeakers()
 {
     using (var connection = new SqlConnection("Data Source=(local); Initial Catalog=MADExpoKJ;Integrated Security=SSPI;"))
     {
         using (var command = new SqlCommand("SELECT * FROM Speaker ORDER BY LastName, FirstName", connection))
         {
             connection.Open();
             using (var reader = command.ExecuteReader())
             {
                 var list = new List<Speaker>();
                 while (reader.Read())
                 {
                     var speaker = new Speaker
                     {
                         SpeakerId = reader.GetInt32(0),
                         FirstName = reader.GetString(1),
                         LastName = reader.GetString(2),
                         Twitter = reader.IsDBNull(3) ? null : reader.GetString(3),
                         Bio = reader.GetString(4)
                     };
                     list.Add(speaker);
                 }
                 return list;
             }
         }
     }
 }
コード例 #2
0
 public void UpdateSpeaker(Speaker speaker)
 {
     if (TryUpdateModel(speaker))
     {
         SpeakerDataProvider.UpdateSpeaker(speaker);
     }
 }
コード例 #3
0
 public static bool UpdateSpeaker(Speaker speaker)
 {
     using (var connection = new SqlConnection("Data Source=(local); Initial Catalog=MADExpoKJ;Integrated Security=SSPI;"))
     {
         using (var command = new SqlCommand("UPDATE Speaker SET FirstName = @FirstName, LastName = @LastName, Twitter = @Twitter WHERE SpeakerId = @SpeakerId", connection))
         {
             command.Parameters.AddWithValue("@SpeakerId", speaker.SpeakerId);
             command.Parameters.AddWithValue("@FirstName", speaker.FirstName);
             command.Parameters.AddWithValue("@LastName", speaker.LastName);
             command.Parameters.AddWithValue("@Twitter", speaker.Twitter == null ? DBNull.Value : (object)speaker.Twitter);
             connection.Open();
             return command.ExecuteNonQuery() > 0;
         }
     }
 }