【问题标题】:How to display sql query results into textbox on c#?如何在c#上将sql查询结果显示到文本框中?
【发布时间】:2016-01-25 06:32:59
【问题描述】:

我目前正在 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?

【问题讨论】:

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


【解决方案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);
     }

 }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      由于您的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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多