//Adds a new file. public void AddFile(File file) { bool isConnOpen = false; string sql = "INSERT INTO userfiles (file_owner, file_title, link) VALUES(@fileOwner, @fileTitle, @link)"; NpgsqlCommand cmd = new NpgsqlCommand(sql, MyConnection); cmd.Parameters.AddWithValue("@fileOwner", file.FileOwner); cmd.Parameters.AddWithValue("@fileTitle", file.FileTitle); cmd.Parameters.AddWithValue("@link", file.Link); if (MyConnection.State == System.Data.ConnectionState.Closed) { MyConnection.Open(); isConnOpen = true; MyTransaction = MyConnection.BeginTransaction(); } if (MyTransaction != null) { //used to participate in an opened trasaction happening somewhere else //assign the Transaction property to the opened transaction cmd.Transaction = MyTransaction; } cmd.ExecuteNonQuery(); MyTransaction.Commit(); if (isConnOpen) { MyConnection.Close(); isConnOpen = false; } }