ExecuteReader:尚未初始化Connection属性。浏览器游戏

时间:2014-05-09 11:29:39

标签: c# asp.net

我们在显示玩家从我们的数据库中获得的体验时遇到了一些问题。

这是一个学校项目,想要一些提示:)

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();
        }

1 个答案:

答案 0 :(得分:2)

您没有关联SqlConnectionSqlCommand

只需将您的连接定义为第二个参数,例如;

SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=@uid", connection);

或者您可以将SqlCommand.Connection属性称为

CheckExp.Connection = connection;