【发布时间】:2013-02-16 07:36:05
【问题描述】:
如果我有一个返回 DataSet 的函数,它有 1 行的 int 值。
如何读取该 int 值?
【问题讨论】:
如果我有一个返回 DataSet 的函数,它有 1 行的 int 值。
如何读取该 int 值?
【问题讨论】:
为什么要使用数据集进行单个静态值。如果您使用的是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();
}
【讨论】: