大家好,有人可以给我帮助我是新手,我不知道该怎么办!该错误指向SqlDataReader rdr = cmd.ExecuteReader(),它说,
System.Data.dll中出现'System.InvalidOperationException'类型的异常,但未在用户代码中处理
我的代码在下面。提前谢谢。
EmpListModel
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; }
}
}
EmpController
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();
}
}
}
Index.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>
答案 0 :(得分:2)
您在创建SqlConnection时有两个using语句。在您的SqlCommand,cmd中,您使用的是第一个连接 - conObj,但是您使用conn.Open()打开了第二个连接。 省略第二个SqlConnection,然后打开第一个。 也可以使用SqlDataReader创建。
编辑:正如NightOwl888所建议的,指出未打开连接的问题,这是更改的代码。 编辑2:我已经将return语句更改为将模型列表包含在返回视图中。
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
}
}
}
答案 1 :(得分:0)
首先,您不需要两个连接来执行此操作。单个连接就可以了。其次,问题似乎与查询返回的字段的数据类型有关。尝试使用'?.ToString()'而不是Convert.ToString。另外,请考虑处理来自数据库的数据类型。例如,DateTime列可以在C#中转换为DateTime,然后您可以使用ToString('format')来提取值。
另一个建议是从数据读取器循环中删除所有列,然后逐个添加,这样就可以隔离导致问题的列。