【问题标题】:how to read int value from dataset? in C# [closed]如何从数据集中读取 int 值?在 C# [关闭]
【发布时间】:2013-02-16 07:36:05
【问题描述】:

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

如何读取该 int 值?

【问题讨论】:

标签: c# sql dataset


【解决方案1】:

为什么要使用数据集进行单个静态值。如果您使用的是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();
}

【讨论】:

  • 抱歉,我没有理解答案。我做了一个函数,它返回 1 行,其中有 1 个值,我想做一些类似 textbox1.text = function().getdata 我该怎么做?
  • 请参考此示例msdn.microsoft.com/en-us/library/… span>
  • @ user2120874而不是读取dataSet中的函数,使用上面引用的ExecuteScalar()直接读取函数结果。 span>
  • 效果很好,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多