我们在显示玩家从我们的数据库中获得的体验时遇到了一些问题。
这是一个学校项目,想要一些提示:)
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=@uid");
string uID = Session["userID"].ToString();
CheckExp.Parameters.AddWithValue("@uid", uID);
try
{
connection.Open();
SqlDataReader ExpReader = null;
ExpReader = CheckExp.ExecuteReader();
if (ExpReader.Read())
{
Label6.Text = ExpReader["Experience"].ToString();
}
}
catch (Exception ex)
{
Label6.Text=(ex.Message);
}
finally
{
connection.Close();
}
答案 0 :(得分:2)
您没有关联SqlConnection
和SqlCommand
。
只需将您的连接定义为第二个参数,例如;
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=@uid", connection);
或者您可以将SqlCommand.Connection
属性称为
CheckExp.Connection = connection;