System.Data.SqlClient.SqlException:无效的列名称“ ErneGaels”

时间:2019-06-19 15:46:57

标签: c# asp.net sql-server visual-studio

我正在从SQL Server数据库读取表数据并收到错误

  

System.Data.SqlClient.SqlException:无效的列名'ErneGaels'   enter image description here

我在SQL查询中为TeamN值使用了字符串值Session["TeamName"],但是它不起作用。

Session["TeamName"]ErneGaels,因为已登录的团队)

每当我在SQL查询中硬编码团队名称的值(例如'ErneGaels')时,它就可以完美工作,并且得到所需的结果。

DataTable dt = new DataTable();

sqlcon.Open();
string TeamN = (string)Session["TeamName"];

SqlDataReader myReader = null;

SqlCommand myCommand = new SqlCommand("SELECT * FROM Registeration WHERE TeamName = " + TeamN, sqlcon);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
    lblID123.Text = "" + (myReader["TeamID"].ToString());
    lblName.Text = "" + (myReader["TeamName"].ToString());
    lblCounty.Text = "" + (myReader["County"].ToString());
    lblEmail.Text = "" + (myReader["Email"].ToString());
    lblPassword.Text = "" + (myReader["Password"].ToString());

    Session["TeamID"] = lblID123.Text.Trim();
}

sqlcon.Close();

我希望能够返回'TeamID'值,以便可以将其分配给会话。

1 个答案:

答案 0 :(得分:0)

尝试如下...

SqlCommand myCommand =新的SqlCommand(“ SELECT * FROM注册位置TeamName = @TeamName”,sqlcon);

myCommand.Parameters.Add(“ @ TeamName”,TeamN)