【发布时间】:2018-07-14 21:17:16
【问题描述】:
我正在尝试使用会话字符串变量作为 SQL 命令中的 WHERE 子句条件,下面是 C# 中的代码
static string strConn = ConfigurationManager.ConnectionStrings["pEAnUtWoRM"].ToString();
SqlConnection conn = new SqlConnection(strConn);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
displayProfiles();
}
}
private void displayProfiles()
{
if (Session["email"] != null )
{
string email = Convert.ToString(Session["email"]);
SqlCommand cmd = new SqlCommand("SELECT Student.Name FROM Student " +
"LEFT JOIN Mentor ON Student.MentorID = Mentor.MentorID " +
"WHERE Mentor.EmailAddr = " + email.ToString(), conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet result = new DataSet();
conn.Open();
da.Fill(result, "StudentProfile");
conn.Close();
gvProfileList.DataSource = result.Tables["StudentProfile"];
gvProfileList.DataBind();
}
}
连接字符串和SqlConnection 工作正常(它不在方法中),因为我已经在其他aspx 页面上对其进行了测试。错误说:
System.Data.SqlClient.SqlException:无法在
da.Fill(result, "StudentProfile");上绑定多部分标识符
【问题讨论】:
-
请不要对我们大喊大叫(粗体字是喊)。我们知道您是新人,因为您的个人资料。我们知道您需要帮助,因为您提出了一个问题。 :o)
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看 Little Bobby Tables
-
感谢大家对我提出问题的方式的反馈以及对我的建议。就像我说的那样,我是新来的,因此我真的不知道提出问题的正确方法,而且我不是一个强大的程序员。
标签: c# sql-server visual-studio session