2016-12-14 69 views
1

我想將標記作爲projPoints1設置爲epual SQL查詢返回的內容。基於SQL查詢結果更改標籤文本

這是我目前擁有的代碼:

using (QC.SqlCommand cmd = new QC.SqlCommand("SELECT *,[proj] FROM[dbo].[WeeklyStats] WHERE[playerID] = @player1 AND [weeks] = @week1 ")) 
{ 
    string week1 = Weeks.SelectedValue.ToString(); 

    cmd.Parameters.AddWithValue("@player1", playerArr[0].ToString()); 
    cmd.Parameters.AddWithValue("@week1", week1); 

    //runs the sql statement and adds them all to the dropdown list 
    cmd.Connection = connection; 
    cmd.CommandType = DT.CommandType.Text; 

    QC.SqlDataReader reader = cmd.ExecuteReader(); 

    projPoints1.Text = ; 
    connection.Close(); 
} 

回答

0

我認爲他們是沒有錯的讀者訪問值,您可以使用這樣的事情:

while(reader.Read()) 
{ 
    projPoints1.Text = reader["proj"].ToString(); 
} 

如果您只需要proj然後你可以通過刪除*來修改查詢,如下所示:SELECT [proj] FROM[dbo].[WeeklyStats] ...這次你可以使用cmd.ExecuteScalar()來獲得該值;代碼將爲:

projPoints1.Text = (string)cmd.ExecuteScalar();