Making a regular method asynchronous in C #

I have a method in C #:

public DataSet OpenSqlQuery(string SqlQuery)
{
    con = new SqlConnection(connection_string);
    con.Open();
    var sqlc = new SqlCommand(SqlQuery, con);
    da = new SqlDataAdapter(sqlc);
    var ds = new DataSet();
    da.Fill(ds);
    con.Close();
    return ds;
}

Now I want to do this as an asynchronous method, so I can name it await. so I changed it to:

public async Task<DataSet> OpenSqlQuery(string SqlQuery)
{
    con = new SqlConnection(connection_string);
    con.Open();
    var sqlc = new SqlCommand(SqlQuery, con);
    da = new SqlDataAdapter(sqlc);
    var ds = new DataSet();
    await da.Fill(ds);
    con.Close();
    return ds;
}

I installed awaitbefore da.Fill(ds), because on this line the program is waiting for the server. But the function shows this error:

'int' does not contain a definition for 'GetAwaiter' and there is no extension to the GetAwaiter method that takes the first argument of type 'int' maybe (do you miss the use directive or assembly reference?)

How can I make this method asynchronous?

PS: The main goal is to call this method as follows:

Dataset ds = await objConnect.OpenSqlQuery(query);

P.S: , , - .

+4

Source: https://habr.com/ru/post/1676134/


All Articles