如何从数据集中读取int值?在C#中

Shm*_*hen -5 c# sql dataset

如果我有一个返回DataSet的函数,它有1行int值.

我怎么能读到那个int值?

SP0*_*007 6

为什么要将数据集用于单个静态值.如果您使用的是sql阅读器,请使用ExecuteScalar函数

例:

function GetIntValue()
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);

    con.Open();

    SqlCommand cmdCount = new SqlCommand("SELECT COUNT(*) FROM Employees",con);
    int numberOfEmployees = (int)cmdCount.ExecuteScalar();
    Response.Write("Here are the " + numberOfEmployees.ToString() + " employees: <br><br>");

    SqlCommand cmd = new SqlCommand("SELECT * FROM Employees",con);
    SqlDataReader dr = cmd.ExecuteReader();
    while(dr.Read()) {
        Response.Write(dr["LastName"] + "<br>");
    }
    con.Close();
}
Run Code Online (Sandbox Code Playgroud)