我正在尝试从Web API
发送数据集响应,并希望通过将Controller
响应从Api转换为数据集,将Json
方法作为数据集接收。
- API方法:
public DataSet GetAllProducts()
{
//return products;
var ds = new DataSet();
var conn = DACUtil.GetConnection();
try
{
SqlCommand cmd = new SqlCommand("Select * from Employee, Company where Employee.Emp_id='2' and Company.Emp_id=2", conn);
var da = new SqlDataAdapter { SelectCommand = cmd };
ds.Tables[0].TableName = "Employee";
ds.Tables[1].TableName = "Company";
da.Fill(ds);
ds.AcceptChanges();
}
finally
{
conn?.Dispose();
}
return ds;
}
- 控制器接收方法:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:57589");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("api/products").Result;
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string jsonS=await content.ReadAsStringAsync();
var ds = JsonConvert.DeserializeObject<DataSet>(jsonS);
}
}
JsonS在Json收到回复,但var ds = JsonConvert.DeserializeObject<DataSet>(jsonS)
;在转换中抛出异常。