I have a dataset that should have only one value in it, what is the best way to get that single value out and into an integer?
SqlCommand.ExecuteScalar
// setup sql command and sql connection etc first...
int count = (int) cmd.ExecuteScalar();
See a new feature of C# 3.0 'Extension Methods' You can define your own extension method for any class. For example:
namespace MyEx;
public static class MyExtensions
{
public static object SingleValue(this DataSet ds)
{
return ds.Tables[0].Rows[0][0];
}
}
After you can use it:
using MyEx;
public class Class1
{
public void Test()
{
DataSet ds = new DataSet();
object value = ds.SingleValue();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With