如何在c#上将sql查询结果显示到文本框中?

时间:2015-10-26 08:02:12

标签: c# sql-server winforms visual-studio textbox

我目前正在使用C#Windows窗体制作投票应用。

所以我做了一个SQL查询来计算有多少人投票选出将在textBox4上显示的特定候选人

 private void button1_Click(object sender, EventArgs e)
 {
     string idcan = textBox3.Text;
     string score = textBox4.Text;
     Connection con = new Connection();
     SqlConnection sqlcon = con.Sambung();

     sqlcon.Open();
     string cek = "select count (ID_Candidate) as Score from DataVote where ID_Candidate = @idcan";

     using (sqlcon)
     {
         SqlCommand com = new SqlCommand(cek, sqlcon);
         com.Parameters.Add("idcan", SqlDbType.VarChar, 5).Value = score;
     }
     try
     {
         sqlcon.Open();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }

 }

如何将上述SQL查询的结果显示到textBox4?

4 个答案:

答案 0 :(得分:1)

 private void button1_Click(object sender, EventArgs e)
 {
     string idcan = textBox3.Text;
     string score = textBox4.Text;
     Connection con = new Connection();
     SqlConnection sqlcon = con.Sambung();

     sqlcon.Open();
     string cek = "select count (ID_Candidate) as Score from DataVote where ID_Candidate = @idcan";

     using (sqlcon)
     {
         SqlCommand com = new SqlCommand(cek, sqlcon);
         com.Parameters.Add("idcan", SqlDbType.VarChar, 5).Value = score;

     }
     try
     {
         sqlcon.Open();
         textBox4.Text=Convert.ToString(com.ExecuteScalar()); 
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }

 }

答案 1 :(得分:1)

您实际上可以执行以下代码:

private void button1_Click(object sender, EventArgs e)
 {

 string idcan = textBox3.Text;
 string score = textBox4.Text;
 Datatable dt = new DataTable();
 Connection con = new Connection();
 SqlConnection sqlcon = con.Sambung();
 SqlCommand com ;

 sqlcon.Open();
 string cek = "select count (ID_Candidate) as Score from DataVote where ID_Candidate = @idcan";  

using(sqlcon)
{
   using(com = new SqlCommand(cek, sqlcon))
   {
     sqlcon.Open();
    com.Parameter.Add("@idcan",score );
    SqlDataAdapter da = new SqlDataAdapter(com);
    da.File(dt);
    if(dt.Rows.Count > 0)
    {
        textBox4.Text = dt.Rows[0]["Score"].ToString();
    }
  }
}
 }

答案 2 :(得分:0)

由于您的SELECT语句返回包含一列的一行,因此您可以使用ExecuteScalar来获取它。

textBox4.Text = com.ExecuteScalar().ToString();

你的代码需要进行一些重构,比如;

using(SqlConnection sqlcon = con.Sambung())
using(SqlCommand com = sqlcon.CreateCommand())
{
    com.CommandText = "select count (ID_Candidate) as Score from DataVote where ID_Candidate = @idcan";
    com.Parameters.Add("@idcan", SqlDbType.VarChar, 5).Value = score;
    sqlcon.Open();
    textBox4.Text = com.ExecuteScalar().ToString();
}

顺便说一下,我强烈怀疑您的ID_Candidate列应该是某种数字类型,而不是VarChar基于它的名称。

答案 3 :(得分:0)

扩展您的问题:如果您的查询以表格形式返回(许多列,多行),您可以使用下面的代码。类似于案例返回单一值。

//Create class to store result value
public class ClassName
{
    public string Col1 { get; set; }
    public int Col2 { get; set; }
}
// In query code
ClassName[] allRecords = null;
SqlCommand command = ...; // your code
using (var reader = command.ExecuteReader())
{
    var list = new List<ClassName>();
    while (reader.Read())
        list.Add(new ClassName {Col1 = reader.GetString(0), Col2 = reader.GetInt32(1)});
    allRecords = list.ToArray();
}