计数从SQL行到C#文本框 [英] Count from SQL Rows into C# textbox
本文介绍了计数从SQL行到C#文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它第一次使用stackoverflow所以每一个L)
Hi there its the first time to use stackoverflow so hi every one L)
我是一个初学者C#形式我把它作为一个有趣的爱好。 / p>
i'm a beginner into C# forms i take it as a fun hobby.
SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Members where sponser = "
+textbox1.text+"'", connection);
Int32 count = (Int32)comm.ExecuteScalar();
textbox2.Text ="Found "+ count+" Members;
之间有两个代码我从google得到了xD
错误出现在这里 textbox2.Text =Found+ count +Members; b $ b
well its just a mix between 2 codes i have got from google xD how ever the error appear here textbox2.Text ="Found "+ count+" Members;
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
lb1.Text = GetRecordCount(textbox2.Text).ToString();
}
private int GetRecordCount(string myParameter)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ToString();
Int32 count = 0;
string sql = "SELECT COUNT(*) FROM members WHERE sponsor = @Sponsor";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Sponsor", SqlDbType.VarChar);
cmd.Parameters["@Sponsor"].Value = myParameter;
try
{
conn.Open();
count = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
}
}
return (int)count;
}
这篇关于计数从SQL行到C#文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文