I have troubles finding out how to save an SQL result into a String or whatever type the result returns.
My SQL query is:
SELECT SUM(Length) FROM tbl_tes
Try calling .ExecuteScalar() on the SqlCommand that you prepare for it. EG:
SqlConnection connection = new SqlConnection(myConnectionString);
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT SUM(Length) FROM tbl_test WHERE TITLE LIKE 't%'";
int result = ((int)cmd.ExecuteScalar());
connection.Close();
You can open and close SqlConnections as much as you like (it's SoP to open and close each time you do an operation, or series of ops in the same method) because .Net will not really close the connection for real, it'll "soft close" it and plop the live connection back into a Connection Pool for recycling later.