-
-
Save Buildstarted/2982541f6fb7990be7f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// POST api/values | |
[HttpPost] | |
[ActionName("Complex")] | |
public HttpResponseMessage PostComplex(TwoField twoField) | |
{ | |
List<string> list = new List<string>(); | |
var name = "undefined"; | |
if(ModelState.IsValid && twoField != null) | |
{ | |
name = twoField.First == null ? | |
"Steve" : HttpUtility.HtmlEncode(twoField.First); | |
Trace.WriteLine("Item was read in successfully. YAY!"); | |
} | |
string connectionString; | |
// this section enables MySQL login credentials to be non-hardcoded | |
System.Configuration.Configuration config = | |
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/"); | |
System.Configuration.ConnectionStringSettings connString; | |
if (config.ConnectionStrings.ConnectionStrings.Count > 0) | |
{ | |
connString = | |
config.ConnectionStrings.ConnectionStrings["WatchTowerConnectionString"]; | |
connectionString = connString.ConnectionString; | |
} | |
else | |
{ | |
connectionString = ""; | |
} | |
var result = new StringBuilder(); | |
using(var connection = new MySqlConnection() | |
{ | |
ConnectionString = connectionString | |
}) { | |
connection.Open(); | |
using(var cmd = new MySqlCommand() { Connection = connection}) { | |
cmd.CommandText = "SELECT name, food FROM demo WHERE name = @name"; | |
cmd.Parameters.AddWithValue("@name", name); | |
using(var reader = cmd.ExecuteReader()) { | |
while (reader.Read()) | |
{ | |
list.Add(reader.GetString("food")); | |
} | |
return Request.CreateResponse(HttpStatusCode.OK, new | |
{ | |
items = list | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment