SQL查询输出未在文本框中查看

时间:2017-07-27 10:21:41

标签: c# .net winforms visual-studio ado.net

我的SQL查询输出没有在我的文本框中查看,当表单加载时它必须自动插入到我的文本框中。我的文本框中唯一的输出是System.Data.SqlClient.SqlCommand

我不知道我的代码有什么不对或缺失。请帮帮我,对不起我只是c#

的新手

非常感谢任何类型的回复。提前谢谢。

private void EmailGen_Load(object sender, EventArgs e)
{
    connect.Open();
    string emailto = "select emailaddress from emails where password = ''";
    string emailfr = "select emailaddress from emails where password != null";
    SqlCommand emailt = new SqlCommand(emailto, connect);
    SqlCommand emailf = new SqlCommand(emailfr, connect);
    emailt.ExecuteNonQuery();

    txBEmailRec.Text = emailt.ToString();
    txBEmailFr.Text = emailf.ToString(); ;
    connect.Close();

    // TODO: This line of code loads data into the 'kwemDataSet.tblProducts' table. You can move, or remove it, as needed.
    this.tblProductsTableAdapter.Fill(this.kwemDataSet.tblProducts);
}

1 个答案:

答案 0 :(得分:1)

您应该使用ExecuteScalar();代替ExecuteNonQuery();而且,您似乎缺少代码来执行emailf SqlCommand

您也可以看到此reference

private void EmailGen_Load(object sender, EventArgs e)
{
    connect.Open();
    string emailto = "select emailaddress from emails where password = ''";
    string emailfr = "select emailaddress from emails where password != null";
    SqlCommand emailt = new SqlCommand(emailto, connect);
    SqlCommand emailf = new SqlCommand(emailfr, connect);

    txBEmailRec.Text = emailt.ExecuteScalar().ToString();
    txBEmailFr.Text = emailf.ExecuteScalar().ToString();
    connect.Close();

    // TODO: This line of code loads data into the 'kwemDataSet.tblProducts' table. You can move, or remove it, as needed.
    this.tblProductsTableAdapter.Fill(this.kwemDataSet.tblProducts);
}