-5

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

我如何读取该 int 值?

4

1 回答 1

6

为什么将数据集用于单个静态值。如果您使用的是 sql reader,请使用 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();
}
于 2013-03-01T17:53:01.247 回答