How to get SQL result saved into a C# variable? -


i have troubles finding out how save sql result string or whatever type result returns.

my sql query is

select sum(length) tbl_test title 't%' 

here need function sums length of datarows title beginns letter "t", when executing query in ms-sql returns "188.99" (the sql type decimal)

my question is, how save value c# variable? query returns 1 row particular value, want save variable. have c# function execute query, need value row, 188.99, saved variable. suggestions cool,no matter if result saved decimal variable or string.

try calling .executescalar() on sqlcommand prepare it. eg:

sqlconnection connection = new sqlconnection(myconnectionstring); sqlcommand cmd = connection.createcommand(); cmd.commandtext = "select sum(length) tbl_test title 't%'";  int result = ((int)cmd.executescalar()); connection.close(); 

you can open , close sqlconnections as (it's sop open , close each time operation, or series of ops in same method) because .net not close connection real, it'll "soft close" , plop live connection connection pool recycling later.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -