2015-10-26 73 views
0

我目前正在C#Windows窗體上投票應用程序。如何在c#中的文本框中顯示sql查詢結果?

所以我做了一個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結果的特定候選人?

+0

也許這可以幫助HTTP://計算器。 COM /問題/ 15294239 /計數從-SQL行 - 進入 - C-銳文本框 – jLaw

回答

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

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

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

實際上,你可以這樣做:

私人無效的button1_Click(對象發件人,EventArgs的){

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

}

相關問題