我的 SqlCommand 有问题,我打开页面时遇到错误:
System.Data.SqlClient.SqlException: Incorrect syntax near 'Van'.
我找不到问题,因为在整个项目和标题中只找到了一次“Van”。这是我在 Page_Load 中的代码:
using (SqlConnection con = new SqlConnection(RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")))
{
var cmd = new SqlCommand("SELECT (SELECT Memo_ID, Dep_Name FROM Department WHERE (Department_ID = Staff.Depar_ID)) AS DepartmentName FROM Staff WHERE (FirstName + SPACE(1) + LastName = " + User.Identity.Name, con);
cmd.Connection.Open();
var sqlReader = cmd.ExecuteReader();
while (sqlReader.Read())
{
String result = sqlReader.GetString(0);
DropDownList1.DataBind();
DropDownList1.Items.FindByValue(result).Selected = true;
//Fill some data like : string result = sqlReader("SomeFieldName");
}
sqlReader.Close();
cmd.Connection.Close();
cmd.Dispose();
}
数据库连接字符串是正确的,因为它适用于我的所有其他页面。我正在尝试获取员工工作的部门,以便他/她只能查看他们自己部门的备忘录。