2013-03-01 152 views
-5

如果我有一个返回DataSet的函数,那有一个int值的行。如何从数据集中读取int值?在C#

我怎样读取int值?

+1

[C#ADO.NET的OleDbCommand - 的ExecuteScalar(http://csharp.net-informations.com/data-providers/csharp-oledbcommand -executescalar.htm) – 2013-03-01 17:52:53

回答

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(); 
} 
+0

对不起,我不明白答案。 我做了一个函数返回我1行1点的值在那里,我想这样做someing像 textbox1.text =()的函数。的GetData 我该怎么办呢? – ShmuelCohen 2013-03-01 17:59:34

+0

请参阅此示例http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx – SP007 2013-03-01 18:00:54

+0

@ user2120874而不是将函数读入DataSet中,请使用ExecuteScalar()作为引用上面将函数结果直接读入一个变量。 – 2013-03-01 18:01:15