用个dataset来接受这条语句的返回值,读dataset就可以读出来count了。SqlConnection conn = new SqlConnection(“server=(local);uid=sa;pwd=;database=your database”); conn.Open(); SqlDataAdapter sd = new SqlDataAdater(“select count(*) f from [Book] group by Type”,conn) DataSet ds = new DataSet(); sd.fill(ds); conn.close();int count = (int)ds.Tables[0].rows[0][0].ToString();
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { conn.Close(); throw; } }
用个dataset来接受这条语句的返回值,读dataset就可以读出来count了。SqlConnection conn = new SqlConnection(“server=(local);uid=sa;pwd=;database=your database”);
conn.Open();
SqlDataAdapter sd = new SqlDataAdater(“select count(*) f from [Book] group by Type”,conn)
DataSet ds = new DataSet();
sd.fill(ds);
conn.close();int count = (int)ds.Tables[0].rows[0][0].ToString();
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}