// 获得数据写入text(主方法)
private void count(object sender, EventArgs e)
{
string str1 = "select * from 表名 where 条件 ";//读取整张表
MySqlConnection coon = MySQL.getMySqlCon();//连接数据库,调方法
MySqlCommand mySqlCommand1 = MySQL.getSqlCommand(str1, coon);
coon.Open();
int record1 = MySQL.getResultCount(mySqlCommand1);
coon.Close();
label1.Text = record1.ToString();
}
//建立执行命令的语句对象
public static MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
return mySqlCommand;
}
//查询记录数
public static int getResultCount(MySqlCommand mySqlCommand)
{
MySqlDataReader reader = mySqlCommand.ExecuteReader();
int result = 0;
try
{
while (reader.Read())
{
if (reader.HasRows)
{
result = result + 1;
}
}
}
catch (Exception)
{
}
finally
{
reader.Close();
}
return result;
}
//此方法连接数据库
public static MySqlConnection getMySqlCon()
{
String mysqlStr = "Database=test3;Data Source=127.0.0.1;User Id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;Allow Zero Datetime=True";
MySqlConnection mysql = new MySqlConnection(mysqlStr);
return mysql;
}
string sql=select count(*) from table where 条件='';
然后写个方法,返回值是个数,然后前台调方法再显示在textbox
int count = (from m in db.Table
where 条件
select m).Count();