C# 如何使用webapi将Mysql表内容转换为json?
我正在使用datatable从数据库中获取表数据。如何将所有这些数据转换为json 我试着在html文件中使用它C# 如何使用webapi将Mysql表内容转换为json?,c#,mysql,.net,json,C#,Mysql,.net,Json,我正在使用datatable从数据库中获取表数据。如何将所有这些数据转换为json 我试着在html文件中使用它 public class theatresController : ApiController { [HttpGet] public static DataTable GetAlltheatredet() { try { string connString = "Server=localhost;databa
public class theatresController : ApiController
{
[HttpGet]
public static DataTable GetAlltheatredet()
{
try
{
string connString = "Server=localhost;database=mytable;uid=root;";
string query = "SELECT TheatreName FROM `mytable`.`theatredetails`";
MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
DataSet DS = new DataSet();
ma.Fill(DS);
return DS.Tables[0];
}
catch (MySqlException e)
{
throw new Exception(e.Message);
}
}
但我有个错误。。。如何实现此功能?我一直在使用此方法
public Stream convertDataSetOJSON(数据集ds){try{byte[]resultBytes=Encoding.UTF8.GetBytes(JsonConvert.Serialized对象(ds,Formatting.Indented));WebOperationContext.Current.OutgoingResponse.ContentType=“text/plain”返回新的MemoryStream(resultBytes);}catch(Exception ex){throw ex;}}}
。使用ORM和/或返回DTO。如果您不希望这样,并且希望获得当前方法的帮助,请至少共享您得到的错误。@CodeCaster:调用Web API时出错。控件无法转到APIControllererror
请求的$.ajax()
函数有参数。使用这些函数可以获得实际的错误。@CodeCaster:我需要在success函数中编写什么?
$(document).ready(function ()
{
$("#Theaterdet").click(function ()
{
alert("in document submit");
$.ajax({
type: "GET",
url: "/api/theatres/",
dataType: 'json',
success: function (data)
{
},
error: function ()
{
alert("Error while invoking the Web API");
}
});
});
});