关于sql数据库统计的一个问题 public int StatMessage(string DataTableName,int mid)
{
int CountMessage;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["xiang"].ConnectionString);
con.Open();
string sql = "select count(*) as count from " + DataTableName + " where MG_id=" + mid;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read())
{
CountMessage = Int32.Parse(dr["count"].ToString()); //设断点看dr能读进来,CountMessage但是值是0
}
else
{
CountMessage = 0;
}
dr.Close();
con.Close(); return CountMessage;
}
哪错了呀???
{
int CountMessage;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["xiang"].ConnectionString);
con.Open();
string sql = "select count(*) as count from " + DataTableName + " where MG_id=" + mid;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read())
{
CountMessage = Int32.Parse(dr["count"].ToString()); //设断点看dr能读进来,CountMessage但是值是0
}
else
{
CountMessage = 0;
}
dr.Close();
con.Close(); return CountMessage;
}
哪错了呀???
{
int CountMessage;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["xiang"].ConnectionString);
con.Open();
string sql = "select count(*) as count from " + DataTableName + " where MG_id=" + mid;
SqlCommand cmd = new SqlCommand(sql, con);
int i=cmd.ExecuteScalar();
if (i>0)
{
return i;
}
else
{
return 0;
}
dr.Close();
con.Close(); 改成这个试试,如果还是0那么数据为里就为0或是你的条件写错了
ExecuteScalar();返回结果集中的第一行第一列的值!
SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read())
{
CountMessage = Int32.Parse(dr["count"].ToString()); //设断点看dr能读进来,CountMessage但是值是0
}
else
{
CountMessage = 0;
}
dr.Close();
con.Close(); return CountMessage;
}