Skip to content

Instantly share code, notes, and snippets.

Created June 12, 2015 00:57
Show Gist options
  • Save anonymous/678247ec214b66cd1599 to your computer and use it in GitHub Desktop.
Save anonymous/678247ec214b66cd1599 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();
var connection = new MySqlConnection()
{
ConnectionString = connectionString
};
connection.Open();
var cmd =
new MySqlCommand()
{
Connection = connection,
CommandText = String.Format("SELECT name, food FROM demo WHERE name = '{0}'", name)
};
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