Reputation: 15757
I have a query that I'm running in C# .cs file:
DataSet ds = db.ExecuteDataSet(System.Data.CommandType.Text, "SELECT count(*) as counter FROM [Table] where [Table].[Field] = 'test'");
I want to do is retrieve the value of "counter" in this query and "return" it as my functions return value.
How can I do that?
Upvotes: 1
Views: 8523
Reputation: 33183
You also shouldn't be using inline SQL. This SELECT count should all be thrown into a stored procedure. The stored procedure should be called from your applications data access layer.
That is where reusability comes in, you start noticing that you just might need to call this function across various places in your web site / client app.
Just some tips!
Upvotes: -2
Reputation: 60674
As the SQL query won't return a dataset but a scalar, you should use the .ExecuteScalar()
method:
int count = (int)db.ExecuteScalar(System.Data.COmmandType.Text, "SELECT count(*) as counter FROM [Table] where [Table].[Field] = 'test'");
(It would be easier for us to provide an answer if you told us what type the db
instance is of...)
Upvotes: 11
Reputation: 81536
DataSet ds = db.ExecuteDataSet(System.Data.CommandType.Text, "SELECT count(*) as counter FROM [Table] where [Table].[Field] = 'test'");
return Convert.ToInt32(ds.Tables[0].Rows[0]["counter"]);
Upvotes: 1