Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Forked from anonymous/gist:678247ec214b66cd1599
Last active August 29, 2015 14:22
Show Gist options
  • Save Buildstarted/2982541f6fb7990be7f6 to your computer and use it in GitHub Desktop.
Save Buildstarted/2982541f6fb7990be7f6 to your computer and use it in GitHub Desktop.
// 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