1
이 메서드는 Razor가있는 ASP.Net의 AJAX에서 실행하려고합니다. 눈에 띄는 오류는 없지만 실행하려고 할 때마다 내게 이것을 보냅니다. 이런 식으로 완벽하게 달리는 방법이 있습니다. 하지만 나는 뭔가를 놓친 것 같아.System.Data.dll에서 SqlException이 발생했지만 사용자 코드에서 처리되지 않았습니다.
이것은 당신이 당신의 명령은 설정하지 말아야 일반 텍스트 SQL 명령 인 경우, 저장 프로 시저를 실행하고자 할 때 당신은 CommandType.StoredProcedure를 사용하여 내 C# 방법
[WebMethod]
public JsonResult GetDeparments()
{
string cs = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
string sql = "SELECT * FROM[DB_PCC].[dbo].[Departments]";
List<Departments> departaments = new List<Departments>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(sql , con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader(); //unhandled expection here
while (rdr.Read())
{
Departments dep = new Departments();
dep.Id = Convert.ToInt32(rdr["Id"]);
dep.Code = rdr["Code"].ToString();
dep.Description = rdr["Description"].ToString();
departaments.Add(dep);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
return Json(new { success = true, message = (js.Serialize(departaments)) },
JsonRequestBehavior.AllowGet);
}
선택 명령은 StoredProcedure가 아닙니다. CommandType을 설정하는 줄을 제거하십시오 – Steve
그 위에'SqlConnection'뿐만 아니라 다른 모든 것에'using '을 적용하고 asp.net mvc에서 필요하지 않은'[WebMethod]'를 제거하고'departments'를 할당하십시오. 수동으로 'JavaScriptSerializer'를 사용하지 않고 직접 (json 응답의 일부만 직렬화한다는 것이 이상하게 느껴지지 않았습니까?). – GSerg