[Help] First real attempt at using async/await and I’m having trouble getting it to work properly.

Below is a code snippet of myself attempting to use asynchronous writes to my local database. At this point, execution never goes beyond

await conn.OpenAsync() 

It's not throwing an exception as it's never breaking in the catch block. I'm pretty certain it's not an issue with the connection string as I can read from the database just fine with the same connection string. I looked up on msdn about how to properly use async/await and from what I can tell, there shouldn't be anything wrong here. However, as soon as I try to open the connection, the program hangs indefinitely, so I'm not sure where to go from here. Any ideas?

 public static async Task<bool> ExecuteSQL(string sql) { int executionValue = 0; try { using(TransactionScope scope = new TransactionScope()) { using(SqlConnection conn = new SqlConnection(_getConnectionString())) { await conn.OpenAsync(); SqlCommand cmd = new SqlCommand(sql, conn); executionValue = await cmd.ExecuteNonQueryAsync(); conn.Close(); } scope.Complete(); } } catch (Exception ex) { string error = ex.Message; } return executionValue != 0; } 

EDIT: Just tried looking at the transaction scope object and found that the constructor can take some async flow options. Those don't appear to be making any difference though.

by krad0n via /r/csharp

Leave a Reply