我在查找代码出现问题时遇到了一些麻烦 调试器命中执行查询时抛出第一次机会异常,我知道这不是查询,因为我在SQL Management Studio中测试了它。如果有人能给我一些有关错误的见解,我们将不胜感激。
这是我的代码,
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
string con_string = WebConfigurationManager.ConnectionStrings["CHDBConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(con_string);
SqlCommand cmd = new SqlCommand("SELECT SUBSTRING(NursingUnitID, 1, 1) AS Floor, COUNT(*) AS Patients" +
"FROM Admissions" +
"WHERE SUBSTRING(NursingUnitID, 1, 1) IN ('1', '2', '3')" +
"AND DischargeDate IS NULL" +
"GROUP BY SUBSTRING(NursingUnitID, 1, 1)", con);
try {
using (con) {
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
chtFloor.Series["Series1"].Name = "currentPatients";
chtFloor.Series["currentPatients"].Points.DataBindXY(reader, "Floor", "Patients");
chtFloor.Width = 600;
chtFloor.Height = 600;
chtFloor.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chtFloor.Titles.Add(new Title("Number of Current Patients on Each Floor", Docking.Top, new Font("Arial", 20f), Color.Black));
chtFloor.Titles.Add(new Title("Nursing Unit", Docking.Bottom, new Font("Arial", 12f), Color.Black));
chtFloor.BackColor = System.Drawing.ColorTranslator.FromHtml("AliceBlue");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
答案 0 :(得分:1)
连接字符串时应添加空格:
SqlCommand cmd = new SqlCommand(
"SELECT SUBSTRING(NursingUnitID, 1, 1) AS Floor, COUNT(*) AS Patients " +
"FROM Admissions " +
"WHERE SUBSTRING(NursingUnitID, 1, 1) IN ('1', '2', '3') " +
"AND DischargeDate IS NULL " +
"GROUP BY SUBSTRING(NursingUnitID, 1, 1)", con);