2014-05-09 209 views
-4

我们遇到一些问题,显示玩家在数据库中的体验。ExecuteReader:连接属性尚未初始化。浏览器游戏

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

SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE [email protected]"); 

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

回答

2

你没有连接您的SqlConnectionSqlCommand

只需将您的连接定义为第二个参数即可;

SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE [email protected]", connection); 

或者你可以指定你的SqlCommand.Connection属性;

CheckExp.Connection = connection; 
+2

我个人建议 - http://meta.stackoverflow.com/a/252531/21061 – Ian

+0

@ user3620202既然你解决你的问题,请删除您的问题,因为它甚至不利于未来的访客。我删除了我的答案。 –

+0

@Ian为什么不呢?删除。 –

相关问题