The C# using statement, SQL, and SqlConnection

前端 未结 5 1107
刺人心
刺人心 2020-12-01 18:53

Is this possible using a using statement C# SQL?

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnectio         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 19:51

    Just write it out explicitely:

    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        using (SqlCommand command = new SqlCommand(queryString, connection))
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }
    catch (Exception e)
    {
        // ...handle, rethrow. Also, you might want to catch
        // more specific exceptions...
    }
    finally
    {
        connection.Close();
    }
    

提交回复
热议问题