-2
這是我獲取百分比的代碼,但返回一個空數組。不知道如何解決它從數據庫獲取數據時出錯
List<Employee> employees = new List<Employee>();
SqlConnection conn = new SqlConnection("data source=***.***.***.***;initial catalog=***;persist security info=True;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework");
using (conn)
{
SqlCommand cmd = new SqlCommand("SELECT (CAST(Availability as int)/CAST(Functionality as int))*100 from Vu_EquipmentInfo where Vu_EquipmentInfo.MonitoringDate like " + year + "-" + month, conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Employee employee = new Employee();
employee.Sanctioned_Post = Convert.ToString(reader\["Availability"\]);
// employee.Fill_Post = Convert.ToChar(reader\["District"\]);
employees.Add(employee);
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(employees));
}
當你在數據庫中運行這個'select'命令時,你會得到什麼? –
像這樣的空陣列[] –
首先,您的查詢是Sql注入攻擊的開放門戶。閱讀關於參數化的問題。其次,你必須在using語句中聲明和實例化SqlConnection。 –