从数据库到Label的数据

时间:2015-12-28 04:51:21

标签: c# sql-server database sqlcommand

我想从我的数据库中获取数据到我的标签。

这是我的代码:

con = new SqlConnection(conS);

try
{
    con.Open();
    string q = "SELECT SearchTerm FROM Sentiment WHERE Sentiment = 'positive'";

    SqlCommand query = new SqlCommand(q, con);

    using (SqlDataReader dr = query.ExecuteReader())
    {
        bool success = dr.Read();

        if (success)
        {
            // Label1.Text = dr.GetString(1);
            label3.Text = dr.GetString(1);
        }
    }

    con.Close();
}
catch (Exception ex)
{
    label3.Text = "Error";
}

但是,当我运行该应用程序时,我的标签显示错误'。这段代码有什么问题?

提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

尝试使用选择查询获取Apple::Apple(Apple const & anotherApple) { // allocate an array to hold pointers to Node array = new Node*[tableSize]; for(int i=0; i<tableSize; i++) { Node* src = anotherApple.array[i]; Node* dest; if (src == NULL) { array[i] = NULL; continue; // no data to copy, continue to next row } // set array[i] since there is at-least one element dest = new Node; dest->data = src->data; array[i] = dest; src = src->next; while(src != NULL) { Node* n = new Node; dest->next = n; dest = n; dest->data = src->data; src = src->next; } dest->next = NULL; } } 记录。

Top 1

 string q = "SELECT TOP 1 SearchTerm FROM Sentiment WHERE Sentiment = 'positive'";

希望它会有所帮助。