Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. var connection = "host=localhost;database=message_db;password=postgres;username=postgres";
  2.  
  3. var message = new Message
  4. {
  5. Subject = "subject",
  6. BodyHtml = "Html",
  7. BodyText = "Text"
  8. };
  9.  
  10. var recipients = new List<Recipient>
  11. {
  12. new Recipient {Address = "jake@bob.com", Name = "Jake Scott", RecipientTypeId = 1},
  13. new Recipient {Address = "ben@example.com", Name = "Ben Scott", RecipientTypeId = 2},
  14. new Recipient {Address = "suki@example.com", Name = "Ben Scott", RecipientTypeId = 3},
  15. };
  16.  
  17. NpgsqlConnection.MapCompositeGlobally<Message>("message_dto");
  18. NpgsqlConnection.MapCompositeGlobally<Recipient>("recipient_dto");
  19.  
  20. using (var connection = new NpgsqlConnection(connectionString))
  21. {
  22. connection.Open();
  23.  
  24. connection.Execute("delete from recipient;");
  25. connection.Execute("delete from address;");
  26. connection.Execute("delete from message;");
  27.  
  28.  
  29. NpgsqlCommand command = connection.CreateCommand();
  30.  
  31. command.CommandType = CommandType.StoredProcedure;
  32. command.CommandText = "create_message";
  33. command.Parameters.AddWithValue("message", message);
  34. command.Parameters.AddWithValue("recipients", recipients);
  35.  
  36. using (NpgsqlDataReader reader = command.ExecuteReader())
  37. {
  38. reader.Read();
  39. var messageId = reader.GetInt64(0);
  40. Log.Information("MessageId: {messageId}", messageId);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement