Guest User

Untitled

a guest
Jan 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult SomeAction(MyModel model) {
  3. if(someCondition = true) {
  4. QueryDatabase();
  5. }
  6. }
  7.  
  8. private void QueryDatabase() {
  9. SqlConnection connection = null;
  10. SqlDataReader dataReader = null;
  11. List<NotificationDetails> lDeviceCredentials = new List<NotificationDetails>();
  12. try {
  13. string connectionString = ConfigurationManager.ConnectionStrings["myEntity"].ConnectionString;
  14. EntityConnectionStringBuilder stringBuilder = new EntityConnectionStringBuilder(connectionString);
  15. string providerConnString = stringBuilder.ProviderConnectionString + ";password=myPassword";
  16. string someString= null;
  17. string otherString = null;
  18.  
  19. using (connection = new SqlConnection(providerConnString)) {
  20. connection.Open();
  21. using (SqlCommand command = new SqlCommand("SELECT myfirstcolumn, mysecondcolumn FROM tblsample WHERE member = 1", connection)) {
  22. using (dataReader = command.ExecuteReader()) {
  23. if (dataReader != null) {
  24. while (dataReader.Read()) {
  25. someString= dataReader.GetString(dataReader.GetOrdinal("myfirstcolumn"));
  26. otherString = dataReader.GetString(dataReader.GetOrdinal("mysecondcolumn"));
  27. }
  28.  
  29. if ((!string.IsNullOrEmpty(someString)) && (!string.IsNullOrEmpty(otherString ))) {
  30. // Do Something
  31. } else if (!string.IsNullOrEmpty(someString)) {
  32. // Do Something
  33. } else if (!string.IsNullOrEmpty(otherString )) {
  34. // Do Something
  35. } else {
  36. // Don't do anything.
  37. }
  38. }
  39. }
  40. }
  41. }
  42. } catch (Exception ex) {
  43. Debug.WriteLine(ex.ToString());
  44. }
  45. }
Add Comment
Please, Sign In to add comment