我一直在尝试通过visual studio 2015中的Web服务连接到Microsoft SQL Server。但是,我在尝试从数据库中检索数据时遇到了困难。
在我的网络配置文件中,我有以下代码:
<add name="SecondlyReadingsContext" connectionString="Data Source=myserver;Initial Catalog=SecondlyReadingsContext;user=sa;password=adminadmin2" providerName="System.Data.SqlClient" />
我假设(可能是错误的)上面的代码与服务器和数据库本身建立了连接。我从以下链接引用了上面的代码: Connecting to SQL server from Restful C# service
在我的Model类中,我有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace HelloWorld.Models.APiModels
{
public class SecondlyReading
{
[Key]
public int Id { get; set; }
[Required]
public int ChannelID { get; set; }
[Required]
public string TimeStamp { get; set; }
[Required]
public float RMSVoltage { get; set; }
[Required]
public float Frequency { get; set; }
[Required]
public float RMSCurrent { get; set; }
[Required]
public float RealPower { get; set; }
[Required]
public float ReactivePower { get; set; }
[Required]
public float ApparentPower { get; set; }
[Required]
public float PowerFactor { get; set; }
[Required]
public string DeviceId { get; set; }
//[ForeignKey("DeviceId")]
//public virtual Device Device { get; set; }
}
}
在我的控制器中,我有这个:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using HelloWorld.Models;
using HelloWorld.Models.APiModels;
namespace HelloWorld.Controllers
{
public class SecondlyReadingsController : ApiController
{
private SecondlyReadingsContext db = new SecondlyReadingsContext();
// GET: api/SecondlyReadings
public IQueryable<SecondlyReading> GetSecondlyReadings()
{
return db.SecondlyReadings;
}
// GET: api/SecondlyReadings/5
[ResponseType(typeof(SecondlyReading))]
public IHttpActionResult GetSecondlyReading(int id)
{
SecondlyReading secondlyReading = db.SecondlyReadings.Find(id);
if (secondlyReading == null)
{
return NotFound();
}
return Ok(secondlyReading);
}
// PUT: api/SecondlyReadings/5
[ResponseType(typeof(void))]
public IHttpActionResult PutSecondlyReading(int id, SecondlyReading secondlyReading)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != secondlyReading.Id)
{
return BadRequest();
}
db.Entry(secondlyReading).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!SecondlyReadingExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/SecondlyReadings
[ResponseType(typeof(SecondlyReading))]
public IHttpActionResult PostSecondlyReading(SecondlyReading secondlyReading)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.SecondlyReadings.Add(secondlyReading);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = secondlyReading.Id }, secondlyReading);
}
// DELETE: api/SecondlyReadings/5
[ResponseType(typeof(SecondlyReading))]
public IHttpActionResult DeleteSecondlyReading(int id)
{
SecondlyReading secondlyReading = db.SecondlyReadings.Find(id);
if (secondlyReading == null)
{
return NotFound();
}
db.SecondlyReadings.Remove(secondlyReading);
db.SaveChanges();
return Ok(secondlyReading);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool SecondlyReadingExists(int id)
{
return db.SecondlyReadings.Count(e => e.Id == id) > 0;
}
}
}
我也被告知我应该在以下的SecondReadingContext.cs文件中进行某种数据库连接,但我不确定如何去做:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace HelloWorld.Models
{
public class SecondlyReadingsContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public SecondlyReadingsContext() : base("name=SecondlyReadingsContext")
{
}
public System.Data.Entity.DbSet<HelloWorld.Models.APiModels.SecondlyReading> SecondlyReadings { get; set; }
}
}
当我通过IIS管理器运行我的服务器并将api / SecondReadings添加到url时,我应该以json格式返回数据,但是,我没有检索任何数据并且它返回一个空白页面(在记事本中) ,因为我在Internet Explorer中运行它。有人会告诉我在我的代码中做错了什么,或者我错过了什么。我已经在这方面工作了一段时间,非常感谢任何帮助。
编辑web.config文件后出现