大家好,有人可以在这里给我帮助我是新手,我不知道该怎么办!错误指向 SqlDataReader rdr = cmd.ExecuteReader() 它说,
System.Data.dll 中出现类型为“System.InvalidOperationException”的异常,但未在用户代码中处理
我的代码在下面。提前致谢。
员工列表模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
namespace AIStestv01.Models
{
public class EmpListModel
{
public string BioID { get; set; }
public string Fullname { get; set; }
public string Dthired { get; set; }
public string DeptID { get; set; }
public string Active { get; set; }
public string NTLogin { get; set; }
}
}
员工控制器
using AIStestv01.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace AIStestv01.Controllers.AdminControllers
{
public class EmpController : Controller
{
// GET: Emp
public ActionResult Index()
{
var con = ConfigurationManager.ConnectionStrings["WOPcon"].ConnectionString;
using (SqlConnection conObj = new SqlConnection(con))
{
SqlCommand cmd = new SqlCommand("wsp_Test01", conObj);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
var model = new List<EmpListModel>();
using (SqlConnection conn = new SqlConnection(con))
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(); //it says this is the error
while (rdr.Read())
{
var emp = new EmpListModel();
emp.BioID = Convert.ToString(rdr["BioID"]);
emp.Fullname = Convert.ToString(rdr["Fullname"]);
emp.Dthired = Convert.ToString(rdr["Dthired"]);
emp.DeptID = Convert.ToString(rdr["DeptID"]);
emp.Active = Convert.ToString(rdr["Active"]);
emp.NTLogin = Convert.ToString(rdr["NTLogin"]);
model.Add(emp);
}
}
}
return View();
}
}
}
索引.cshtml
@model IEnumerable<AIStestv01.Models.EmpListModel>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table>
<tr>
<th>Bio ID</th>
<th>FUllname</th>
<th>Date Hired</th>
<th>Dept ID</th>
<th>Active</th>
<th>NT LOgin</th>
</tr>
@foreach (var emp in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => emp.BioID)</td>
<td>@Html.DisplayFor(modelItem => emp.Fullname)</td>
<td>@Html.DisplayFor(modelItem => emp.Dthired)</td>
<td>@Html.DisplayFor(modelItem => emp.DeptID)</td>
<td>@Html.DisplayFor(modelItem => emp.Active)</td>
<td>@Html.DisplayFor(modelItem => emp.NTLogin)</td>
</tr>
}
</table>
您有两个 using 语句与 SqlConnection 创建。在您的 SqlCommand、cmd 中,您使用的是第一个连接 - conObj,但您使用 conn.Open() 打开了第二个连接。省略第二个 SqlConnection using,并打开第一个。也可以使用 SqlDataReader 创建。
编辑:正如 NightOwl888 所建议的指出未打开连接的问题,这是更改后的代码。编辑 2:我已更改返回语句以将模型列表包含到返回视图中。
using AIStestv01.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace AIStestv01.Controllers.AdminControllers
{
public class EmpController : Controller
{
// GET: Emp
public ActionResult Index()
{
var con = ConfigurationManager.ConnectionStrings["WOPcon"].ConnectionString;
using (SqlConnection conObj = new SqlConnection(con))
{
SqlCommand cmd = new SqlCommand("wsp_Test01", conObj);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
var model = new List<EmpListModel>();
conObj.Open();//Openning of the connection associated with the sql command
using (SqlDataReader rdr = cmd.ExecuteReader())//Using around the SqlDataReader to dispose it after use
{
while (rdr.Read())
{
var emp = new EmpListModel();
emp.BioID = Convert.ToString(rdr["BioID"]);
emp.Fullname = Convert.ToString(rdr["Fullname"]);
emp.Dthired = Convert.ToString(rdr["Dthired"]);
emp.DeptID = Convert.ToString(rdr["DeptID"]);
emp.Active = Convert.ToString(rdr["Active"]);
emp.NTLogin = Convert.ToString(rdr["NTLogin"]);
model.Add(emp);
}
}
}
//do something with the
return View(model);//added model to use it in cshtml
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句