[英]asp.net web api with Mysql database
我有PHP的背景,并在尝试ASP.NET webAPI时遇到问题。
我有一个MySQL数据库和一个包含一些文件信息的表。 现在我想创建一个“SELECT * FROM Files”并以XML格式查看结果。
如何呈现返回的查询结果?
这是我目前的代码,
该模型:
namespace ProductsApp.Models
{
public class File
{
public int Id {get; set;}
public string Text {get; set;}
}
}
控制者:
public IEnumerable<File> Get()
{
try
{
command = conn.CreateCommand();
command.CommandText = "SELECT * FROM Files";
conn.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//How to output the rows ????
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null; // return the list
}
对文件表做一些假设,这就是获取数据的方式
public IEnumerable<File> Get()
{
List<File> list = new List<File>();
try
{
command = conn.CreateCommand();
command.CommandText = "SELECT * FROM Files";
conn.Open();
using(MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//How to output the rows ????
int id = (int) reader["Id"];//Assuming column name is 'Id' and value if typeof(int)
string text = (string) reader["Text"];//Assuming column name is `Text` and typeof(string)
var file = new File {Id = id, Text = text};
list.Add(file);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return list; // return the list
}
至于xml是需要在请求上允许的格式设置。
一旦发出调用的客户端请求text/xml
作为内容类型,则给定WebApi的默认设置,然后将结果解析为xml并返回给客户端。
如果您想强制响应始终为xml,那么您需要对响应进行一些更改。 一种方法是在返回结果之前设置Content-Type
标头。
Response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml;charset=utf-8");
还有其他方法可以做到这一点,有很多答案可以告诉你。
Webconfig:
public static MySqlConnection conn()
{
string conn_string = "server=localhost;port=3306;database=testmvc;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(conn_string);
return conn;
}
控制器:
public MySqlConnection con = WebApiConfig.conn();
public IHttpActionResult GetAllProduct()
{
IList<product> pro = null;
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from product", con);
cmd.CommandType = CommandType.Text;
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
pro = ds.Tables[0].AsEnumerable().Select(dataRow => new product { Pname = dataRow.Field<string>("pname"), Pid = dataRow.Field<int>("pid"), Pprice = dataRow.Field<decimal>("pprice") }).ToList();
}
finally
{
con.Close();
}
if (pro.Count == 0)
{
return NotFound();
}
return Ok(pro);
}
public IHttpActionResult PostNewProduct(product pro)
{
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT MAX(pid) from product";
cmd.CommandType = CommandType.Text;
int maxid = Convert.ToInt16(cmd.ExecuteScalar().ToString())+1;
cmd.CommandText = "insert into product values(" + maxid + ",'" + pro.Pname + "'," + pro.Pprice + ")";
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
return Ok();
}
public IHttpActionResult PutOldProduct(product pro)
{
string sql = "update product set pname='" + pro.Pname + "',pprice=" + pro.Pprice + " where pid=" + pro.Pid + "";
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
return Ok();
}
public IHttpActionResult Delete(int id)
{
string sql = "delete from product where pid=" + id + "";
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
return Ok();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.